blob: e197f99ac91733ae69ca23580ea73bdbac61709c [file] [log] [blame]
Brian Silverman431500a2013-10-28 19:50:15 -07001#include "frc971/constants.h"
2
3#include <math.h>
4#include <stdint.h>
5#include <inttypes.h>
6
Brian Silverman0a151c92014-05-02 15:28:44 -07007#include <map>
8
9#if __has_feature(address_sanitizer)
10#include "sanitizer/lsan_interface.h"
11#endif
12
Brian Silverman431500a2013-10-28 19:50:15 -070013#include "aos/common/logging/logging.h"
14#include "aos/common/once.h"
15#include "aos/common/network/team_number.h"
Brian Silverman0a151c92014-05-02 15:28:44 -070016#include "aos/common/mutex.h"
Brian Silverman431500a2013-10-28 19:50:15 -070017
Brian Silverman2c590c32013-11-04 18:08:54 -080018#include "frc971/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h"
Brian Silverman2c590c32013-11-04 18:08:54 -080019#include "frc971/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Brian Silverman2c590c32013-11-04 18:08:54 -080020
Brian Silverman431500a2013-10-28 19:50:15 -070021#ifndef M_PI
22#define M_PI 3.14159265358979323846
23#endif
24
25namespace frc971 {
26namespace constants {
27namespace {
28
Brian Silvermane5db0c62014-03-13 15:53:18 -070029const uint16_t kCompTeamNumber = 971;
Brian Silvermana20703b2014-03-20 14:29:37 -070030const uint16_t kPracticeTeamNumber = 9971;
Comran Morshed79bd3db2015-02-07 14:51:13 +000031
32// ///// Drivetrain Constants
Brian Silvermane5db0c62014-03-13 15:53:18 -070033
Daniel Pettia7827412015-02-13 20:55:57 -080034// These three constants were set by Daniel on 2/13/15.
35const double kDrivetrainEncoderRatio = 20.0 / 64.0;
36const double kLowGearRatio = kDrivetrainEncoderRatio * 20.0 / 50.0;
37const double kHighGearRatio = kLowGearRatio;
Brian Silverman1a6590d2013-11-04 14:46:46 -080038
Brian Silvermane5db0c62014-03-13 15:53:18 -070039const ShifterHallEffect kCompRightDriveShifter{555, 657, 660, 560, 0.2, 0.7};
40const ShifterHallEffect kCompLeftDriveShifter{555, 660, 644, 552, 0.2, 0.7};
Brian Silverman6eb51f12013-11-02 14:39:01 -070041
Austin Schuh010eb812014-10-25 18:06:49 -070042const ShifterHallEffect kPracticeRightDriveShifter{2.95, 3.95, 3.95, 2.95, 0.2, 0.7};
43const ShifterHallEffect kPracticeLeftDriveShifter{2.95, 4.2, 3.95, 3.0, 0.2, 0.7};
Brian Silvermane5db0c62014-03-13 15:53:18 -070044
Daniel Pettia7827412015-02-13 20:55:57 -080045// Set by Daniel on 2/13/15.
46// Distance from the center of the left wheel to the center of the right wheel.
47const double kRobotWidth = 37.806 /*inches*/ * 0.0254;
Brian Silverman6eb51f12013-11-02 14:39:01 -070048
Comran Morshed79bd3db2015-02-07 14:51:13 +000049// ///// Superstructure Constants
50
Comran Morshed79bd3db2015-02-07 14:51:13 +000051// Elevator gearbox pulley output constants.
52const int kElevatorGearboxOutputPulleyTeeth = 32; // 32 teeth
53const double kElevatorGearboxOutputPitch = 0.005; // 5 mm/tooth
Daniel Pettia7827412015-02-13 20:55:57 -080054const double kElevatorGearboxOutputRadianDistance =
55 kElevatorGearboxOutputPulleyTeeth * kElevatorGearboxOutputPitch /
56 (2.0 * M_PI);
Comran Morshed79bd3db2015-02-07 14:51:13 +000057
Austin Schuhdbd6bfa2015-02-14 21:25:16 -080058const double kMaxAllowedLeftRightArmDifference = 0.04; // radians
Austin Schuh703b8d42015-02-01 14:56:34 -080059const double kMaxAllowedLeftRightElevatorDifference = 0.01; // meters
60
Daniel Pettia7827412015-02-13 20:55:57 -080061// Gearing ratios of the pots and encoders for the elevator and arm.
62// Ratio is output shaft rotations per encoder/pot rotation
63// Checked by Daniel on 2/13/15.
64const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
65const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
66const double kElevatorEncoderRatio = 14.0 / 84.0;
67const double kElevatorPotRatio = 1.0;
68const double kClawEncoderRatio = 18.0 / 72.0;
69const double kClawPotRatio = 18.0 / 72.0;
70
Comran Morshed79bd3db2015-02-07 14:51:13 +000071// Number of radians between each index pulse on the arm.
Daniel Pettia7827412015-02-13 20:55:57 -080072const double kArmEncoderIndexDifference = 2.0 * M_PI * kArmEncoderRatio;
73// Number of meters between each index pulse on the elevator.
Comran Morshed79bd3db2015-02-07 14:51:13 +000074const double kElevatorEncoderIndexDifference =
Daniel Pettia7827412015-02-13 20:55:57 -080075 kElevatorEncoderRatio *
76 2.0 * M_PI * // radians
77 kElevatorGearboxOutputRadianDistance;
78// Number of radians between index pulses on the claw.
Comran Morshed79bd3db2015-02-07 14:51:13 +000079const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
80
81const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070082
Daniel Petti9cf68c82015-02-14 14:57:17 -080083// TODO(danielp): All these values might need to change.
84const double kClawPistonSwitchTime = 0.4;
85const double kClawZeroingRange = 0.3;
86
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000087const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070088 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080089 case 1: // for tests
90 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -080091 kDrivetrainEncoderRatio,
92 kArmEncoderRatio,
93 kArmPotRatio,
94 kElevatorEncoderRatio,
95 kElevatorPotRatio,
96 kElevatorGearboxOutputRadianDistance,
97 kClawEncoderRatio,
98 kClawPotRatio,
99 kLowGearRatio,
100 kHighGearRatio,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800101 kCompLeftDriveShifter,
102 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -0800103 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700104 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -0800105 control_loops::MakeVelocityDrivetrainLoop,
106 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000107 0.02, // drivetrain done delta
108 5.0, // drivetrain max speed
109
Comran Morshed79bd3db2015-02-07 14:51:13 +0000110 // Motion ranges: hard_lower_limit, hard_upper_limit,
111 // soft_lower_limit, soft_upper_limit
112 // TODO(sensors): Get actual bounds before turning on robot.
113 {
114 // Claw values, in radians.
115 // 0 is level with the ground.
116 // Positive moves in the direction of positive encoder values.
117 {0.0000000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800118 0.1000000000, 1.2000000000},
119
120 // Zeroing constants for wrist.
Daniel Petti9cf68c82015-02-14 14:57:17 -0800121 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.35},
122
Brian Silverman5d712fc2015-02-15 03:39:31 -0500123 0.0,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800124 kClawPistonSwitchTime,
125 kClawZeroingRange
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800126 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000127
128 {
129 // Elevator values, in meters.
Austin Schuh703b8d42015-02-01 14:56:34 -0800130 // TODO(austin): Fix this. Positive is up.
Comran Morshed79bd3db2015-02-07 14:51:13 +0000131 // 0 is at the top of the elevator frame.
132 // Positive is down towards the drivebase.
133 {0.0000000000, 0.6790000000,
134 0.2000000000, 0.6000000000},
135
136 // Arm values, in radians.
137 // 0 is sticking straight out horizontally over the intake/front.
138 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800139 {-1.570000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800140 -1.200000000, 1.2000000000},
141
142 // Elevator zeroing constants: left, right.
143 // TODO(sensors): Get actual offsets for these.
144 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
145 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
146 // Arm zeroing constants: left, right.
147 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
148 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
Brian Silverman5d712fc2015-02-15 03:39:31 -0500149 0.0, 0.0, 0.0, 0.0,
Austin Schuh703b8d42015-02-01 14:56:34 -0800150 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000151 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800152
153 kMaxAllowedLeftRightArmDifference,
154 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800155 };
156 break;
Brian Silverman431500a2013-10-28 19:50:15 -0700157 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800158 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800159 kDrivetrainEncoderRatio,
160 kArmEncoderRatio,
161 kArmPotRatio,
162 kElevatorEncoderRatio,
163 kElevatorPotRatio,
164 kElevatorGearboxOutputRadianDistance,
165 kClawEncoderRatio,
166 kClawPotRatio,
167 kLowGearRatio,
168 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800169 kCompLeftDriveShifter,
170 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700171 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700172 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800173 control_loops::MakeVelocityDrivetrainLoop,
174 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000175 0.02, // drivetrain done delta
176 5.0, // drivetrain max speed
177
Comran Morshed79bd3db2015-02-07 14:51:13 +0000178 // Motion ranges: hard_lower_limit, hard_upper_limit,
179 // soft_lower_limit, soft_upper_limit
180 // TODO(sensors): Get actual bounds before turning on robot.
181 {
182 // Claw values, in radians.
183 // 0 is level with the ground.
184 // Positive moves in the direction of positive encoder values.
185 {0.0000000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800186 0.1000000000, 1.2000000000},
187
188 // Zeroing constants for wrist.
Daniel Pettia7827412015-02-13 20:55:57 -0800189 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800190
Brian Silverman5d712fc2015-02-15 03:39:31 -0500191 0.0,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800192 kClawPistonSwitchTime,
193 kClawZeroingRange
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800194 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000195
196 {
197 // Elevator values, in meters.
198 // 0 is at the top of the elevator frame.
199 // Positive is down towards the drivebase.
200 {0.0000000000, 0.6790000000,
201 0.2000000000, 0.6000000000},
202
203 // Arm values, in radians.
204 // 0 is sticking straight out horizontally over the intake/front.
205 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800206 {-1.570000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800207 -1.200000000, 1.2000000000},
208
209 // Elevator zeroing constants: left, right.
210 // TODO(sensors): Get actual offsets for these.
211 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
212 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
213 // Arm zeroing constants: left, right.
214 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
215 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
Brian Silverman5d712fc2015-02-15 03:39:31 -0500216 0.0, 0.0, 0.0, 0.0,
Austin Schuh703b8d42015-02-01 14:56:34 -0800217 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000218 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800219
220 kMaxAllowedLeftRightArmDifference,
221 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800222 };
Brian Silverman431500a2013-10-28 19:50:15 -0700223 break;
224 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800225 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800226 kDrivetrainEncoderRatio,
227 kArmEncoderRatio,
228 kArmPotRatio,
229 kElevatorEncoderRatio,
230 kElevatorPotRatio,
231 kElevatorGearboxOutputRadianDistance,
232 kClawEncoderRatio,
233 kClawPotRatio,
234 kLowGearRatio,
235 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800236 kPracticeLeftDriveShifter,
237 kPracticeRightDriveShifter,
238 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700239 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800240 control_loops::MakeVelocityDrivetrainLoop,
241 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000242 0.02, // drivetrain done delta
243 5.0, // drivetrain max speed
244
Comran Morshed79bd3db2015-02-07 14:51:13 +0000245 // Motion ranges: hard_lower_limit, hard_upper_limit,
246 // soft_lower_limit, soft_upper_limit
247 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuhbfb8b242015-02-16 15:45:22 -0800248 {// Claw values, in radians.
249 // 0 is level with the ground.
250 // Positive moves in the direction of positive encoder values.
251 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800252
Austin Schuhbfb8b242015-02-16 15:45:22 -0800253 // Zeroing constants for wrist.
254 // TODO(sensors): Get actual offsets for these.
255 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.977913},
256 6.1663463999999992,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800257
258 kClawPistonSwitchTime,
259 kClawZeroingRange
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800260 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000261
Austin Schuhbfb8b242015-02-16 15:45:22 -0800262 {// Elevator values, in meters.
263 // 0 is at the top of the elevator frame.
264 // Positive is down towards the drivebase.
265 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000266
Austin Schuhbfb8b242015-02-16 15:45:22 -0800267 // Arm values, in radians.
268 // 0 is sticking straight out horizontally over the intake/front.
269 // Positive is rotating up and into the robot (towards the back).
270 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800271
Austin Schuhbfb8b242015-02-16 15:45:22 -0800272 // Elevator zeroing constants: left, right.
273 // TODO(sensors): Get actual offsets for these.
274 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.016041 + 0.001290},
275 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.011367 + 0.003216},
276 // Arm zeroing constants: left, right.
277 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.312677},
278 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.40855},
279 0.72069366666666679 - 0.026008,
280 -0.078959636363636357 - 0.024646,
281 -3.4952331578947375 + 0.011776,
282 3.5263507647058816 - 0.018921 + 0.006545,
Austin Schuh703b8d42015-02-01 14:56:34 -0800283 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000284 // TODO(sensors): End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800285
286 kMaxAllowedLeftRightArmDifference,
287 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800288 };
Brian Silverman431500a2013-10-28 19:50:15 -0700289 break;
290 default:
291 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
292 }
293}
294
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000295const Values *DoGetValues() {
296 uint16_t team = ::aos::network::GetTeamNumber();
297 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
298 return DoGetValuesForTeam(team);
299}
300
Brian Silverman431500a2013-10-28 19:50:15 -0700301} // namespace
302
303const Values &GetValues() {
304 static ::aos::Once<const Values> once(DoGetValues);
305 return *once.Get();
306}
307
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000308const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700309 static ::aos::Mutex mutex;
310 ::aos::MutexLocker locker(&mutex);
311
312 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
313 // race conditions.
314 static ::std::map<uint16_t, const Values *> values;
315
316 if (values.count(team_number) == 0) {
317 values[team_number] = DoGetValuesForTeam(team_number);
318#if __has_feature(address_sanitizer)
319 __lsan_ignore_object(values[team_number]);
320#endif
321 }
322 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000323}
324
Brian Silverman431500a2013-10-28 19:50:15 -0700325} // namespace constants
326} // namespace frc971