blob: 295a8006dfd3f880bbb29e245c04dac883687e48 [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;
73 intake->zeroing.index_difference = Values::kIntakeEncoderIndexDifference;
74 intake->zeroing.measured_index_position = 0;
75 intake->zeroing.allowable_encoder_error = 0.3;
76
77 turret->zeroing.average_filter_size = Values::kZeroingSampleSize;
78 turret->zeroing.index_difference = Values::kTurretEncoderIndexDifference;
79 turret->zeroing.measured_index_position = 0;
80 turret->zeroing.allowable_encoder_error = 0.3;
81
82 hood->zeroing.average_filter_size = Values::kZeroingSampleSize;
83 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
84 hood->zeroing.measured_index_position = 0.1;
85 hood->zeroing.allowable_encoder_error = 0.3;
86
Tyler Chatow6107aba2017-01-22 01:39:40 +000087 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080088 // A set of constants for tests.
89 case 1:
90 intake->pot_offset = 0;
91 turret->pot_offset = 0;
92 hood->pot_offset = 0.1;
93 r->down_error = 0;
94 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +000095 break;
96
97 case kCompTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -080098 intake->pot_offset = 0;
99 turret->pot_offset = 0;
100 hood->pot_offset = 0.1;
101 r->down_error = 0;
102 r->vision_name = "competition";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000103 break;
104
105 case kPracticeTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -0800106 intake->pot_offset = 0;
107 turret->pot_offset = 0;
108 hood->pot_offset = 0.1;
109 r->down_error = 0;
110 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000111 break;
112
113 default:
114 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
115 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800116
117 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000118}
119
120const Values *DoGetValues() {
121 uint16_t team = ::aos::network::GetTeamNumber();
122 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
123 return DoGetValuesForTeam(team);
124}
125
126} // namespace
127
128const Values &GetValues() {
129 static ::aos::Once<const Values> once(DoGetValues);
130 return *once.Get();
131}
132
133const Values &GetValuesForTeam(uint16_t team_number) {
134 static ::aos::Mutex mutex;
135 ::aos::MutexLocker locker(&mutex);
136
137 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
138 // race conditions.
139 static ::std::map<uint16_t, const Values *> values;
140
141 if (values.count(team_number) == 0) {
142 values[team_number] = DoGetValuesForTeam(team_number);
143#if __has_feature(address_sanitizer)
144 __lsan_ignore_object(values[team_number]);
145#endif
146 }
147 return *values[team_number];
148}
149
150} // namespace constants
151} // namespace y2017