blob: 4f873127cf85629d526cfd76dec658a7e2e5ea34 [file] [log] [blame]
Comran Morshed6c6a0a92016-01-17 12:45:16 +00001#include "y2016/constants.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +00002
3#include <math.h>
4#include <stdint.h>
5#include <inttypes.h>
6
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"
14#include "aos/common/once.h"
15#include "aos/common/network/team_number.h"
16#include "aos/common/mutex.h"
17
Comran Morshed6c6a0a92016-01-17 12:45:16 +000018#include "y2016/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
19#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020
21#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
Comran Morshed6c6a0a92016-01-17 12:45:16 +000025namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000026namespace constants {
27namespace {
28
29const uint16_t kCompTeamNumber = 971;
30const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed9a9948c2016-01-16 15:58:04 +000031
Comran Morshed6c6a0a92016-01-17 12:45:16 +000032// TODO(constants): Update these to what we're using this year.
Comran Morshed9a9948c2016-01-16 15:58:04 +000033const double kCompDrivetrainEncoderRatio =
34 (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/;
35const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0;
36const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0;
37
38const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio;
39const double kPracticeLowGearRatio = kCompLowGearRatio;
40const double kPracticeHighGearRatio = kCompHighGearRatio;
41
42const ShifterHallEffect kCompLeftDriveShifter{2.61, 2.33, 4.25, 3.28, 0.2, 0.7};
Comran Morshed6c6a0a92016-01-17 12:45:16 +000043const ShifterHallEffect kCompRightDriveShifter{2.94, 4.31, 4.32,
44 3.25, 0.2, 0.7};
Comran Morshed9a9948c2016-01-16 15:58:04 +000045
Comran Morshed6c6a0a92016-01-17 12:45:16 +000046const ShifterHallEffect kPracticeLeftDriveShifter{2.80, 3.05, 4.15,
47 3.2, 0.2, 0.7};
48const ShifterHallEffect kPracticeRightDriveShifter{2.90, 3.75, 3.80,
49 2.98, 0.2, 0.7};
Comran Morshed9a9948c2016-01-16 15:58:04 +000050
51const double kRobotWidth = 25.0 / 100.0 * 2.54;
52
Austin Schuh2fc10fa2016-02-08 00:44:34 -080053// TODO(comran): Remove comments from ratios for pots when they are actually
54// used.
55const double kIntakeEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
56//const double kIntakePotRatio = 48.0 / 48.0 * 16.0 / 72.0;
57const double kShoulderEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
58//const double kShoulderPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
59const double kWristEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
60//const double kWristPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
61
62const double kIntakeEncoderIndexDifference = 2.0 * M_PI * kIntakeEncoderRatio;
63const double kShoulderEncoderIndexDifference = 2.0 * M_PI * kShoulderEncoderRatio;
64const double kWristEncoderIndexDifference = 2.0 * M_PI * kWristEncoderRatio;
65
66const int kZeroingSampleSize = 200;
67
Comran Morshed9a9948c2016-01-16 15:58:04 +000068const Values *DoGetValuesForTeam(uint16_t team) {
69 switch (team) {
70 case 1: // for tests
71 return new Values{
72 kCompDrivetrainEncoderRatio,
73 kCompLowGearRatio,
74 kCompHighGearRatio,
75 kCompLeftDriveShifter,
76 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000077 0.5,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000078 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
79 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
80 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080081
82 // Intake
83 {
84 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
85 {kZeroingSampleSize, kIntakeEncoderIndexDifference, 0.9, 0.3},
86 },
87
88 // Shoulder
89 {
90 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
91 {kZeroingSampleSize, kShoulderEncoderIndexDifference, 0.9, 0.3},
92 },
93
94 // Wrist
95 {
96 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
97 {kZeroingSampleSize, kWristEncoderIndexDifference, 0.9, 0.3},
98 },
Comran Morshed9a9948c2016-01-16 15:58:04 +000099 };
100 break;
101 case kCompTeamNumber:
102 return new Values{
103 kCompDrivetrainEncoderRatio,
104 kCompLowGearRatio,
105 kCompHighGearRatio,
106 kCompLeftDriveShifter,
107 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000108 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000109 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
110 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
111 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800112
113 // Intake
114 {
115 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
116 {kZeroingSampleSize, kIntakeEncoderIndexDifference, 0.9, 0.3},
117 },
118
119 // Shoulder
120 {
121 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
122 {kZeroingSampleSize, kShoulderEncoderIndexDifference, 0.9, 0.3},
123 },
124
125 // Wrist
126 {
127 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
128 {kZeroingSampleSize, kWristEncoderIndexDifference, 0.9, 0.3},
129 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000130 };
131 break;
132 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000133 return new Values{
134 kPracticeDrivetrainEncoderRatio,
135 kPracticeLowGearRatio,
136 kPracticeHighGearRatio,
137 kPracticeLeftDriveShifter,
138 kPracticeRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000139 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000140 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
141 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
142 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800143
144 // Intake
145 {
146 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
147 {kZeroingSampleSize, kIntakeEncoderIndexDifference, 0.9, 0.3},
148 },
149
150 // Shoulder
151 {
152 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
153 {kZeroingSampleSize, kShoulderEncoderIndexDifference, 0.9, 0.3},
154 },
155
156 // Wrist
157 {
158 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
159 {kZeroingSampleSize, kWristEncoderIndexDifference, 0.9, 0.3},
160 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000161 };
162 break;
163 default:
164 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
165 }
166}
167
168const Values *DoGetValues() {
169 uint16_t team = ::aos::network::GetTeamNumber();
170 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
171 return DoGetValuesForTeam(team);
172}
173
174} // namespace
175
176const Values &GetValues() {
177 static ::aos::Once<const Values> once(DoGetValues);
178 return *once.Get();
179}
180
181const Values &GetValuesForTeam(uint16_t team_number) {
182 static ::aos::Mutex mutex;
183 ::aos::MutexLocker locker(&mutex);
184
185 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
186 // race conditions.
187 static ::std::map<uint16_t, const Values *> values;
188
189 if (values.count(team_number) == 0) {
190 values[team_number] = DoGetValuesForTeam(team_number);
191#if __has_feature(address_sanitizer)
192 __lsan_ignore_object(values[team_number]);
193#endif
194 }
195 return *values[team_number];
196}
197
198} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000199} // namespace y2016