blob: 008e08bbc9e1553d0f5b1bc104a3051c4a7ae617 [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;
65
66 r->drivetrain_max_speed = 5;
67
68 intake->zeroing.average_filter_size = Values::kZeroingSampleSize;
Adam Snaider79900c22017-02-08 20:23:15 -080069 intake->zeroing.one_revolution_distance = Values::kIntakeEncoderIndexDifference;
Austin Schuh0fc1e6d2017-02-21 02:04:10 -080070 intake->zeroing.zeroing_threshold = 0.0005;
71 intake->zeroing.moving_buffer_size = 20;
Brian Silvermana10d20a2017-02-19 14:28:53 -080072 intake->zeroing.allowable_encoder_error = 0.3;
Brian Silvermandb8498a2017-02-11 17:16:09 -080073
Austin Schuh6a90cd92017-02-19 20:55:33 -080074 hood->zeroing.index_pulse_count = 2;
Brian Silvermandb8498a2017-02-11 17:16:09 -080075 hood->zeroing.index_difference = Values::kHoodEncoderIndexDifference;
Austin Schuh6a90cd92017-02-19 20:55:33 -080076 hood->zeroing.known_index_pulse = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -080077
Tyler Chatow6107aba2017-01-22 01:39:40 +000078 switch (team) {
Brian Silvermandb8498a2017-02-11 17:16:09 -080079 // A set of constants for tests.
80 case 1:
81 intake->pot_offset = 0;
Austin Schuh6a90cd92017-02-19 20:55:33 -080082 intake->zeroing.measured_absolute_position = 0;
83
Brian Silvermandb8498a2017-02-11 17:16:09 -080084 hood->pot_offset = 0.1;
Austin Schuh6a90cd92017-02-19 20:55:33 -080085 hood->zeroing.measured_index_position = 0.05;
86
Brian Silvermandb8498a2017-02-11 17:16:09 -080087 r->down_error = 0;
88 r->vision_name = "test";
Tyler Chatow6107aba2017-01-22 01:39:40 +000089 break;
90
91 case kCompTeamNumber:
Austin Schuh23cc9ed2017-02-24 19:14:06 -080092 intake->pot_offset = 0.26712;
93 intake->zeroing.measured_absolute_position = 0.008913;
94
Austin Schuh23cc9ed2017-02-24 19:14:06 -080095 hood->zeroing.measured_index_position = 0.652898 - 0.488117;
96
97 r->down_error = 0;
98 r->vision_name = "competition";
99 break;
100
101 case kPracticeTeamNumber:
Austin Schuhe6baace2017-03-04 15:05:02 -0800102 intake->pot_offset = 0.2921 + 0.00039 + 0.012236 - 0.023602;
103 intake->zeroing.measured_absolute_position = 0.031437;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800104
Austin Schuh0fc1e6d2017-02-21 02:04:10 -0800105 hood->zeroing.measured_index_position = 0.655432 - 0.460505;
Austin Schuh6a90cd92017-02-19 20:55:33 -0800106
Brian Silvermandb8498a2017-02-11 17:16:09 -0800107 r->down_error = 0;
Brian Silvermandb8498a2017-02-11 17:16:09 -0800108 r->vision_name = "practice";
Tyler Chatow6107aba2017-01-22 01:39:40 +0000109 break;
110
Austin Schuh23cc9ed2017-02-24 19:14:06 -0800111
Tyler Chatow6107aba2017-01-22 01:39:40 +0000112 default:
113 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
114 }
Brian Silvermandb8498a2017-02-11 17:16:09 -0800115
116 return r;
Tyler Chatow6107aba2017-01-22 01:39:40 +0000117}
118
119const Values *DoGetValues() {
120 uint16_t team = ::aos::network::GetTeamNumber();
121 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
122 return DoGetValuesForTeam(team);
123}
124
125} // namespace
126
127const Values &GetValues() {
128 static ::aos::Once<const Values> once(DoGetValues);
129 return *once.Get();
130}
131
132const Values &GetValuesForTeam(uint16_t team_number) {
133 static ::aos::Mutex mutex;
134 ::aos::MutexLocker locker(&mutex);
135
136 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
137 // race conditions.
138 static ::std::map<uint16_t, const Values *> values;
139
140 if (values.count(team_number) == 0) {
141 values[team_number] = DoGetValuesForTeam(team_number);
142#if __has_feature(address_sanitizer)
143 __lsan_ignore_object(values[team_number]);
144#endif
145 }
146 return *values[team_number];
147}
148
149} // namespace constants
150} // namespace y2017