blob: 20bafaeb3d611b1572542b00ff6d2f9922ad1ad0 [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 -080053const int kZeroingSampleSize = 200;
54
Austin Schuh04c894e2016-02-13 23:54:42 -080055constexpr Values::Range kIntakeRange{// lower hard stop
56 -0.4,
57 // upper hard stop
58 2,
59 // lower soft limit
60 -0.3,
61 // upper soft limit
62 1.9};
63constexpr Values::Range kShoulderRange{// lower hard stop
64 -0.2,
65 // upper hard stop
66 2.0,
67 // lower soft limit
68 -0.1,
69 // upper soft limit
70 1.9};
71constexpr Values::Range kWristRange{// lower hard stop
72 -2,
73 // upper hard stop
74 2,
75 // lower soft limit
76 -1.9,
77 // upper soft limit
78 1.9};
79
Comran Morshed9a9948c2016-01-16 15:58:04 +000080const Values *DoGetValuesForTeam(uint16_t team) {
81 switch (team) {
82 case 1: // for tests
83 return new Values{
84 kCompDrivetrainEncoderRatio,
85 kCompLowGearRatio,
86 kCompHighGearRatio,
87 kCompLeftDriveShifter,
88 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000089 0.5,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000090 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
91 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
92 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080093
94 // Intake
95 {
Austin Schuh04c894e2016-02-13 23:54:42 -080096 kIntakeRange,
Austin Schuh0efb1952016-02-13 22:58:17 -080097 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080098 },
99
100 // Shoulder
101 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800102 kShoulderRange,
Austin Schuh0efb1952016-02-13 22:58:17 -0800103 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800104 },
105
106 // Wrist
107 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800108 kWristRange,
Austin Schuh0efb1952016-02-13 22:58:17 -0800109 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.0, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800110 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000111 };
112 break;
113 case kCompTeamNumber:
114 return new Values{
115 kCompDrivetrainEncoderRatio,
116 kCompLowGearRatio,
117 kCompHighGearRatio,
118 kCompLeftDriveShifter,
119 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000120 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000121 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
122 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
123 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800124
125 // Intake
126 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800127 kIntakeRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800128 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800129 },
130
131 // Shoulder
132 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800133 kShoulderRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800134 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800135 },
136
137 // Wrist
138 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800139 kWristRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800140 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800141 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000142 };
143 break;
144 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000145 return new Values{
146 kPracticeDrivetrainEncoderRatio,
147 kPracticeLowGearRatio,
148 kPracticeHighGearRatio,
149 kPracticeLeftDriveShifter,
150 kPracticeRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000151 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000152 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
153 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
154 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800155
156 // Intake
157 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800158 kIntakeRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800159 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800160 },
161
162 // Shoulder
163 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800164 kShoulderRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800165 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800166 },
167
168 // Wrist
169 {
Austin Schuh04c894e2016-02-13 23:54:42 -0800170 kWristRange,
Austin Schuhc5467a12016-02-13 20:25:13 -0800171 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800172 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 };
174 break;
175 default:
176 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
177 }
178}
179
180const Values *DoGetValues() {
181 uint16_t team = ::aos::network::GetTeamNumber();
182 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
183 return DoGetValuesForTeam(team);
184}
185
186} // namespace
187
188const Values &GetValues() {
189 static ::aos::Once<const Values> once(DoGetValues);
190 return *once.Get();
191}
192
193const Values &GetValuesForTeam(uint16_t team_number) {
194 static ::aos::Mutex mutex;
195 ::aos::MutexLocker locker(&mutex);
196
197 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
198 // race conditions.
199 static ::std::map<uint16_t, const Values *> values;
200
201 if (values.count(team_number) == 0) {
202 values[team_number] = DoGetValuesForTeam(team_number);
203#if __has_feature(address_sanitizer)
204 __lsan_ignore_object(values[team_number]);
205#endif
206 }
207 return *values[team_number];
208}
209
210} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000211} // namespace y2016