blob: 56798a2d89bfbce71564b50a8e8c81c65555f105 [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
Daniel Pettie1bb13e2015-02-17 13:59:15 -080058const double kArmZeroingHeight = 0.2;
59
Austin Schuhdbd6bfa2015-02-14 21:25:16 -080060const double kMaxAllowedLeftRightArmDifference = 0.04; // radians
Austin Schuh703b8d42015-02-01 14:56:34 -080061const double kMaxAllowedLeftRightElevatorDifference = 0.01; // meters
62
Daniel Pettia7827412015-02-13 20:55:57 -080063// Gearing ratios of the pots and encoders for the elevator and arm.
64// Ratio is output shaft rotations per encoder/pot rotation
65// Checked by Daniel on 2/13/15.
66const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
67const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
68const double kElevatorEncoderRatio = 14.0 / 84.0;
69const double kElevatorPotRatio = 1.0;
70const double kClawEncoderRatio = 18.0 / 72.0;
71const double kClawPotRatio = 18.0 / 72.0;
72
Comran Morshed79bd3db2015-02-07 14:51:13 +000073// Number of radians between each index pulse on the arm.
Daniel Pettia7827412015-02-13 20:55:57 -080074const double kArmEncoderIndexDifference = 2.0 * M_PI * kArmEncoderRatio;
75// Number of meters between each index pulse on the elevator.
Comran Morshed79bd3db2015-02-07 14:51:13 +000076const double kElevatorEncoderIndexDifference =
Daniel Pettia7827412015-02-13 20:55:57 -080077 kElevatorEncoderRatio *
78 2.0 * M_PI * // radians
79 kElevatorGearboxOutputRadianDistance;
80// Number of radians between index pulses on the claw.
Comran Morshed79bd3db2015-02-07 14:51:13 +000081const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
82
83const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070084
Daniel Petti9cf68c82015-02-14 14:57:17 -080085// TODO(danielp): All these values might need to change.
86const double kClawPistonSwitchTime = 0.4;
87const double kClawZeroingRange = 0.3;
88
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000089const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070090 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080091 case 1: // for tests
92 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -080093 kDrivetrainEncoderRatio,
94 kArmEncoderRatio,
95 kArmPotRatio,
96 kElevatorEncoderRatio,
97 kElevatorPotRatio,
98 kElevatorGearboxOutputRadianDistance,
99 kClawEncoderRatio,
100 kClawPotRatio,
101 kLowGearRatio,
102 kHighGearRatio,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800103 kCompLeftDriveShifter,
104 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -0800105 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700106 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -0800107 control_loops::MakeVelocityDrivetrainLoop,
108 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000109 0.02, // drivetrain done delta
110 5.0, // drivetrain max speed
111
Comran Morshed79bd3db2015-02-07 14:51:13 +0000112 // Motion ranges: hard_lower_limit, hard_upper_limit,
113 // soft_lower_limit, soft_upper_limit
114 // TODO(sensors): Get actual bounds before turning on robot.
115 {
116 // Claw values, in radians.
117 // 0 is level with the ground.
118 // Positive moves in the direction of positive encoder values.
119 {0.0000000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800120 0.1000000000, 1.2000000000},
121
122 // Zeroing constants for wrist.
Daniel Petti9cf68c82015-02-14 14:57:17 -0800123 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.35},
124
Brian Silverman5d712fc2015-02-15 03:39:31 -0500125 0.0,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800126 kClawPistonSwitchTime,
127 kClawZeroingRange
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800128 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000129
130 {
131 // Elevator values, in meters.
Austin Schuh703b8d42015-02-01 14:56:34 -0800132 // TODO(austin): Fix this. Positive is up.
Comran Morshed79bd3db2015-02-07 14:51:13 +0000133 // 0 is at the top of the elevator frame.
134 // Positive is down towards the drivebase.
135 {0.0000000000, 0.6790000000,
136 0.2000000000, 0.6000000000},
137
138 // Arm values, in radians.
139 // 0 is sticking straight out horizontally over the intake/front.
140 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800141 {-1.570000000, 1.5700000000,
Daniel Pettia7827412015-02-13 20:55:57 -0800142 -1.200000000, 1.2000000000},
143
144 // Elevator zeroing constants: left, right.
145 // TODO(sensors): Get actual offsets for these.
146 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
147 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
148 // Arm zeroing constants: left, right.
149 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
150 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
Brian Silverman5d712fc2015-02-15 03:39:31 -0500151 0.0, 0.0, 0.0, 0.0,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800152
153 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800154 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000155 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800156
157 kMaxAllowedLeftRightArmDifference,
158 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800159 };
160 break;
Brian Silverman431500a2013-10-28 19:50:15 -0700161 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800162 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800163 kDrivetrainEncoderRatio,
164 kArmEncoderRatio,
165 kArmPotRatio,
166 kElevatorEncoderRatio,
167 kElevatorPotRatio,
168 kElevatorGearboxOutputRadianDistance,
169 kClawEncoderRatio,
170 kClawPotRatio,
171 kLowGearRatio,
172 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800173 kCompLeftDriveShifter,
174 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700175 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700176 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800177 control_loops::MakeVelocityDrivetrainLoop,
178 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000179 0.02, // drivetrain done delta
180 5.0, // drivetrain max speed
181
Comran Morshed79bd3db2015-02-07 14:51:13 +0000182 // Motion ranges: hard_lower_limit, hard_upper_limit,
183 // soft_lower_limit, soft_upper_limit
184 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuh0b406bb2015-02-17 02:28:48 -0800185 {// Claw values, in radians.
186 // 0 is level with the ground.
187 // Positive moves in the direction of positive encoder values.
188 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800189
Austin Schuh0b406bb2015-02-17 02:28:48 -0800190 // Zeroing constants for wrist.
191 {kZeroingSampleSize, kClawEncoderIndexDifference,
192 0.9104180000000001},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800193
Austin Schuh0b406bb2015-02-17 02:28:48 -0800194 6.308141,
195 kClawPistonSwitchTime,
196 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000197
Austin Schuh0b406bb2015-02-17 02:28:48 -0800198 {// Elevator values, in meters.
199 // 0 is the portion of the elevator carriage that Spencer removed
200 // lining up with the bolt.
201 // Positive is up.
202 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000203
Austin Schuh0b406bb2015-02-17 02:28:48 -0800204 // Arm values, in radians.
205 // 0 is sticking straight out horizontally over the intake/front.
206 // Positive is rotating up and into the robot (towards the back).
207 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800208
Austin Schuh0b406bb2015-02-17 02:28:48 -0800209 // Elevator zeroing constants: left, right.
210 // TODO(sensors): Get actual offsets for these.
211 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.058672},
212 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.078646},
213 // Arm zeroing constants: left, right.
214 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.324437},
215 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.064683},
216 0.722230,
217 -0.081354,
218 -3.509611 - 0.007415 - -0.019081,
219 3.506927 - 0.170017 - -0.147970,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800220 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800221 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000222 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800223
224 kMaxAllowedLeftRightArmDifference,
225 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800226 };
Brian Silverman431500a2013-10-28 19:50:15 -0700227 break;
228 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800229 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800230 kDrivetrainEncoderRatio,
231 kArmEncoderRatio,
232 kArmPotRatio,
233 kElevatorEncoderRatio,
234 kElevatorPotRatio,
235 kElevatorGearboxOutputRadianDistance,
236 kClawEncoderRatio,
237 kClawPotRatio,
238 kLowGearRatio,
239 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800240 kPracticeLeftDriveShifter,
241 kPracticeRightDriveShifter,
242 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700243 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800244 control_loops::MakeVelocityDrivetrainLoop,
245 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000246 0.02, // drivetrain done delta
247 5.0, // drivetrain max speed
248
Comran Morshed79bd3db2015-02-07 14:51:13 +0000249 // Motion ranges: hard_lower_limit, hard_upper_limit,
250 // soft_lower_limit, soft_upper_limit
251 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuhbfb8b242015-02-16 15:45:22 -0800252 {// Claw values, in radians.
253 // 0 is level with the ground.
254 // Positive moves in the direction of positive encoder values.
255 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800256
Austin Schuhbfb8b242015-02-16 15:45:22 -0800257 // Zeroing constants for wrist.
258 // TODO(sensors): Get actual offsets for these.
Austin Schuh8a436e82015-02-16 23:31:28 -0800259 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.973311},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800260 6.1663463999999992,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800261
262 kClawPistonSwitchTime,
263 kClawZeroingRange
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800264 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000265
Austin Schuhbfb8b242015-02-16 15:45:22 -0800266 {// Elevator values, in meters.
267 // 0 is at the top of the elevator frame.
268 // Positive is down towards the drivebase.
269 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000270
Austin Schuhbfb8b242015-02-16 15:45:22 -0800271 // Arm values, in radians.
272 // 0 is sticking straight out horizontally over the intake/front.
273 // Positive is rotating up and into the robot (towards the back).
274 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800275
Austin Schuhbfb8b242015-02-16 15:45:22 -0800276 // Elevator zeroing constants: left, right.
277 // TODO(sensors): Get actual offsets for these.
278 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.016041 + 0.001290},
279 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.011367 + 0.003216},
280 // Arm zeroing constants: left, right.
281 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.312677},
282 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.40855},
283 0.72069366666666679 - 0.026008,
284 -0.078959636363636357 - 0.024646,
285 -3.4952331578947375 + 0.011776,
286 3.5263507647058816 - 0.018921 + 0.006545,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800287
288 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800289 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000290 // TODO(sensors): End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800291
292 kMaxAllowedLeftRightArmDifference,
293 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800294 };
Brian Silverman431500a2013-10-28 19:50:15 -0700295 break;
296 default:
297 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
298 }
299}
300
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000301const Values *DoGetValues() {
302 uint16_t team = ::aos::network::GetTeamNumber();
303 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
304 return DoGetValuesForTeam(team);
305}
306
Brian Silverman431500a2013-10-28 19:50:15 -0700307} // namespace
308
309const Values &GetValues() {
310 static ::aos::Once<const Values> once(DoGetValues);
311 return *once.Get();
312}
313
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000314const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700315 static ::aos::Mutex mutex;
316 ::aos::MutexLocker locker(&mutex);
317
318 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
319 // race conditions.
320 static ::std::map<uint16_t, const Values *> values;
321
322 if (values.count(team_number) == 0) {
323 values[team_number] = DoGetValuesForTeam(team_number);
324#if __has_feature(address_sanitizer)
325 __lsan_ignore_object(values[team_number]);
326#endif
327 }
328 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000329}
330
Brian Silverman431500a2013-10-28 19:50:15 -0700331} // namespace constants
332} // namespace frc971