blob: 3e228e80c8a2920fa4dcbe3c58752bfa0b9ed81b [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
Comran Morshed9a9948c2016-01-16 15:58:04 +000055const Values *DoGetValuesForTeam(uint16_t team) {
56 switch (team) {
57 case 1: // for tests
58 return new Values{
59 kCompDrivetrainEncoderRatio,
60 kCompLowGearRatio,
61 kCompHighGearRatio,
62 kCompLeftDriveShifter,
63 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000064 0.5,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000065 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
66 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
67 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080068
69 // Intake
70 {
Austin Schuhc5467a12016-02-13 20:25:13 -080071 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
72 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080073 },
74
75 // Shoulder
76 {
Austin Schuhc5467a12016-02-13 20:25:13 -080077 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
78 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080079 },
80
81 // Wrist
82 {
Austin Schuhc5467a12016-02-13 20:25:13 -080083 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
84 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -080085 },
Comran Morshed9a9948c2016-01-16 15:58:04 +000086 };
87 break;
88 case kCompTeamNumber:
89 return new Values{
90 kCompDrivetrainEncoderRatio,
91 kCompLowGearRatio,
92 kCompHighGearRatio,
93 kCompLeftDriveShifter,
94 kCompRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +000095 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +000096 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
97 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
98 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -080099
100 // Intake
101 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800102 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
103 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800104 },
105
106 // Shoulder
107 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800108 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
109 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800110 },
111
112 // Wrist
113 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800114 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
115 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800116 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000117 };
118 break;
119 case kPracticeTeamNumber:
Comran Morshed9a9948c2016-01-16 15:58:04 +0000120 return new Values{
121 kPracticeDrivetrainEncoderRatio,
122 kPracticeLowGearRatio,
123 kPracticeHighGearRatio,
124 kPracticeLeftDriveShifter,
125 kPracticeRightDriveShifter,
Comran Morshed9a9948c2016-01-16 15:58:04 +0000126 kRobotWidth,
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000127 ::y2016::control_loops::drivetrain::MakeVelocityDrivetrainLoop,
128 ::y2016::control_loops::drivetrain::MakeDrivetrainLoop,
129 5.0, // drivetrain max speed
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800130
131 // Intake
132 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800133 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
134 {kZeroingSampleSize, INTAKE_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800135 },
136
137 // Shoulder
138 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800139 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
140 {kZeroingSampleSize, SHOULDER_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800141 },
142
143 // Wrist
144 {
Austin Schuhc5467a12016-02-13 20:25:13 -0800145 {-M_PI - 0.05, M_PI + 0.05, -M_PI, M_PI},
146 {kZeroingSampleSize, WRIST_ENCODER_INDEX_DIFFERENCE, 0.9, 0.3},
Austin Schuh2fc10fa2016-02-08 00:44:34 -0800147 },
Comran Morshed9a9948c2016-01-16 15:58:04 +0000148 };
149 break;
150 default:
151 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
152 }
153}
154
155const Values *DoGetValues() {
156 uint16_t team = ::aos::network::GetTeamNumber();
157 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
158 return DoGetValuesForTeam(team);
159}
160
161} // namespace
162
163const Values &GetValues() {
164 static ::aos::Once<const Values> once(DoGetValues);
165 return *once.Get();
166}
167
168const Values &GetValuesForTeam(uint16_t team_number) {
169 static ::aos::Mutex mutex;
170 ::aos::MutexLocker locker(&mutex);
171
172 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
173 // race conditions.
174 static ::std::map<uint16_t, const Values *> values;
175
176 if (values.count(team_number) == 0) {
177 values[team_number] = DoGetValuesForTeam(team_number);
178#if __has_feature(address_sanitizer)
179 __lsan_ignore_object(values[team_number]);
180#endif
181 }
182 return *values[team_number];
183}
184
185} // namespace constants
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000186} // namespace y2016