blob: 05eca31cebca8026ab3c63fd382710968083487c [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 Silvermandb8498a2017-02-11 17:16:09 -080030constexpr double Values::kDrivetrainEncoderRatio;
31
32constexpr double Values::kShooterEncoderRatio;
33
34constexpr double Values::kIntakeEncoderRatio, Values::kIntakePotRatio,
35 Values::kIntakeEncoderIndexDifference;
36constexpr ::frc971::constants::Range Values::kIntakeRange;
37
38constexpr double Values::kHoodEncoderRatio, Values::kHoodPotRatio,
39 Values::kHoodEncoderIndexDifference;
40constexpr ::frc971::constants::Range Values::kHoodRange;
41
42constexpr double Values::kTurretEncoderRatio, Values::kTurretPotRatio,
43 Values::kTurretEncoderIndexDifference;
44constexpr ::frc971::constants::Range Values::kTurretRange;
45
46constexpr double Values::kIndexerEncoderRatio,
47 Values::kIndexerEncoderIndexDifference;
Tyler Chatow6107aba2017-01-22 01:39:40 +000048
49namespace {
Brian Silvermandb8498a2017-02-11 17:16:09 -080050
Tyler Chatow6107aba2017-01-22 01:39:40 +000051const uint16_t kCompTeamNumber = 971;
52const uint16_t kPracticeTeamNumber = 9971;
53
Tyler Chatow6107aba2017-01-22 01:39:40 +000054const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080055 Values *const r = new Values();
56 Values::Intake *const intake = &r->intake;
57 Values::Turret *const turret = &r->turret;
58 Values::Hood *const hood = &r->hood;
59
60 r->drivetrain_max_speed = 5;
61
62 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
63 intake->zeroing.index_difference = Values::kIntakeEncoderIndexDifference;
64 intake->zeroing.measured_index_position = 0;
65 intake->zeroing.allowable_encoder_error = 0.3;
66
67 turret->zeroing.average_filter_size = Values::kZeroingSampleSize;
68 turret->zeroing.index_difference = Values::kTurretEncoderIndexDifference;
69 turret->zeroing.measured_index_position = 0;
70 turret->zeroing.allowable_encoder_error = 0.3;
71
72 hood->zeroing.average_filter_size = Values::kZeroingSampleSize;
73 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
74 hood->zeroing.measured_index_position = 0.1;
75 hood->zeroing.allowable_encoder_error = 0.3;
76
Tyler Chatow6107aba2017-01-22 01:39:40 +000077 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080078 // A set of constants for tests.
79 case 1:
80 intake->pot_offset = 0;
81 turret->pot_offset = 0;
82 hood->pot_offset = 0.1;
83 r->down_error = 0;
84 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +000085 break;
86
87 case kCompTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -080088 intake->pot_offset = 0;
89 turret->pot_offset = 0;
90 hood->pot_offset = 0.1;
91 r->down_error = 0;
92 r->vision_name = "competition";
Tyler Chatow6107aba2017-01-22 01:39:40 +000093 break;
94
95 case kPracticeTeamNumber:
Brian Silvermandb8498a2017-02-11 17:16:09 -080096 intake->pot_offset = 0;
97 turret->pot_offset = 0;
98 hood->pot_offset = 0.1;
99 r->down_error = 0;
100 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000101 break;
102
103 default:
104 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
105 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800106
107 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000108}
109
110const Values *DoGetValues() {
111 uint16_t team = ::aos::network::GetTeamNumber();
112 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
113 return DoGetValuesForTeam(team);
114}
115
116} // namespace
117
118const Values &GetValues() {
119 static ::aos::Once<const Values> once(DoGetValues);
120 return *once.Get();
121}
122
123const Values &GetValuesForTeam(uint16_t team_number) {
124 static ::aos::Mutex mutex;
125 ::aos::MutexLocker locker(&mutex);
126
127 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
128 // race conditions.
129 static ::std::map<uint16_t, const Values *> values;
130
131 if (values.count(team_number) == 0) {
132 values[team_number] = DoGetValuesForTeam(team_number);
133#if __has_feature(address_sanitizer)
134 __lsan_ignore_object(values[team_number]);
135#endif
136 }
137 return *values[team_number];
138}
139
140} // namespace constants
141} // namespace y2017