blob: ac893915a0ed906f90828a271661e1a645b22118 [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
Daniel Pettiee4fa802015-02-17 10:39:27 -080042const ShifterHallEffect kPracticeRightDriveShifter{2.95, 3.95, 3.95,
43 2.95, 0.2, 0.7};
44const ShifterHallEffect kPracticeLeftDriveShifter{2.95, 4.2, 3.95,
45 3.0, 0.2, 0.7};
Daniel Pettidfc90ba2015-02-17 21:42:15 -080046// TODO(danielp): Actually measure one of them...
47const double kToteHeight = 0.5;
Brian Silvermane5db0c62014-03-13 15:53:18 -070048
Daniel Pettia7827412015-02-13 20:55:57 -080049// Set by Daniel on 2/13/15.
50// Distance from the center of the left wheel to the center of the right wheel.
51const double kRobotWidth = 37.806 /*inches*/ * 0.0254;
Brian Silverman6eb51f12013-11-02 14:39:01 -070052
Comran Morshed79bd3db2015-02-07 14:51:13 +000053// ///// Superstructure Constants
54
Comran Morshed79bd3db2015-02-07 14:51:13 +000055// Elevator gearbox pulley output constants.
56const int kElevatorGearboxOutputPulleyTeeth = 32; // 32 teeth
57const double kElevatorGearboxOutputPitch = 0.005; // 5 mm/tooth
Daniel Pettia7827412015-02-13 20:55:57 -080058const double kElevatorGearboxOutputRadianDistance =
59 kElevatorGearboxOutputPulleyTeeth * kElevatorGearboxOutputPitch /
60 (2.0 * M_PI);
Comran Morshed79bd3db2015-02-07 14:51:13 +000061
Daniel Pettie1bb13e2015-02-17 13:59:15 -080062const double kArmZeroingHeight = 0.2;
63
Daniel Pettiee4fa802015-02-17 10:39:27 -080064const double kMaxAllowedLeftRightArmDifference = 0.04; // radians
Austin Schuh703b8d42015-02-01 14:56:34 -080065const double kMaxAllowedLeftRightElevatorDifference = 0.01; // meters
66
Daniel Pettia7827412015-02-13 20:55:57 -080067// Gearing ratios of the pots and encoders for the elevator and arm.
68// Ratio is output shaft rotations per encoder/pot rotation
69// Checked by Daniel on 2/13/15.
70const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
71const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
72const double kElevatorEncoderRatio = 14.0 / 84.0;
73const double kElevatorPotRatio = 1.0;
74const double kClawEncoderRatio = 18.0 / 72.0;
75const double kClawPotRatio = 18.0 / 72.0;
76
Comran Morshed79bd3db2015-02-07 14:51:13 +000077// Number of radians between each index pulse on the arm.
Daniel Pettiee4fa802015-02-17 10:39:27 -080078const double kArmEncoderIndexDifference = 2.0 * M_PI * kArmEncoderRatio;
Daniel Pettia7827412015-02-13 20:55:57 -080079// Number of meters between each index pulse on the elevator.
Comran Morshed79bd3db2015-02-07 14:51:13 +000080const double kElevatorEncoderIndexDifference =
Daniel Pettiee4fa802015-02-17 10:39:27 -080081 kElevatorEncoderRatio * 2.0 * M_PI * // radians
Daniel Pettia7827412015-02-13 20:55:57 -080082 kElevatorGearboxOutputRadianDistance;
83// Number of radians between index pulses on the claw.
Comran Morshed79bd3db2015-02-07 14:51:13 +000084const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
85
86const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070087
Daniel Petti9cf68c82015-02-14 14:57:17 -080088// TODO(danielp): All these values might need to change.
89const double kClawPistonSwitchTime = 0.4;
90const double kClawZeroingRange = 0.3;
91
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000092const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070093 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080094 case 1: // for tests
95 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -080096 kDrivetrainEncoderRatio,
97 kArmEncoderRatio,
98 kArmPotRatio,
99 kElevatorEncoderRatio,
100 kElevatorPotRatio,
101 kElevatorGearboxOutputRadianDistance,
102 kClawEncoderRatio,
103 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800104 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800105 kLowGearRatio,
106 kHighGearRatio,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800107 kCompLeftDriveShifter,
108 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -0800109 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700110 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -0800111 control_loops::MakeVelocityDrivetrainLoop,
112 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000113 0.02, // drivetrain done delta
114 5.0, // drivetrain max speed
115
Comran Morshed79bd3db2015-02-07 14:51:13 +0000116 // Motion ranges: hard_lower_limit, hard_upper_limit,
117 // soft_lower_limit, soft_upper_limit
118 // TODO(sensors): Get actual bounds before turning on robot.
Daniel Pettiee4fa802015-02-17 10:39:27 -0800119 {// Claw values, in radians.
120 // 0 is level with the ground.
121 // Positive moves in the direction of positive encoder values.
122 {0.0000000000, 1.5700000000, 0.1000000000, 1.2000000000},
Daniel Pettia7827412015-02-13 20:55:57 -0800123
Daniel Pettiee4fa802015-02-17 10:39:27 -0800124 // Zeroing constants for wrist.
125 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.35},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800126
Daniel Pettiee4fa802015-02-17 10:39:27 -0800127 0.0,
128 kClawPistonSwitchTime,
129 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000130
Daniel Pettiee4fa802015-02-17 10:39:27 -0800131 {// Elevator values, in meters.
132 // TODO(austin): Fix this. Positive is up.
133 // 0 is at the top of the elevator frame.
134 // Positive is down towards the drivebase.
135 {0.0000000000, 0.6790000000, 0.2000000000, 0.6000000000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000136
Daniel Pettiee4fa802015-02-17 10:39:27 -0800137 // Arm values, in radians.
138 // 0 is sticking straight out horizontally over the intake/front.
139 // Positive is rotating up and into the robot (towards the back).
140 {-1.570000000, 1.5700000000, -1.200000000, 1.2000000000},
Daniel Pettia7827412015-02-13 20:55:57 -0800141
Daniel Pettiee4fa802015-02-17 10:39:27 -0800142 // 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},
149 0.0,
150 0.0,
151 0.0,
152 0.0,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800153
Daniel Pettiee4fa802015-02-17 10:39:27 -0800154 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800155 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000156 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800157
158 kMaxAllowedLeftRightArmDifference,
159 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800160 };
161 break;
Brian Silverman431500a2013-10-28 19:50:15 -0700162 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800163 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800164 kDrivetrainEncoderRatio,
165 kArmEncoderRatio,
166 kArmPotRatio,
167 kElevatorEncoderRatio,
168 kElevatorPotRatio,
169 kElevatorGearboxOutputRadianDistance,
170 kClawEncoderRatio,
171 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800172 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800173 kLowGearRatio,
174 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800175 kCompLeftDriveShifter,
176 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700177 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700178 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800179 control_loops::MakeVelocityDrivetrainLoop,
180 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000181 0.02, // drivetrain done delta
182 5.0, // drivetrain max speed
183
Comran Morshed79bd3db2015-02-07 14:51:13 +0000184 // Motion ranges: hard_lower_limit, hard_upper_limit,
185 // soft_lower_limit, soft_upper_limit
186 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuh0b406bb2015-02-17 02:28:48 -0800187 {// Claw values, in radians.
188 // 0 is level with the ground.
189 // Positive moves in the direction of positive encoder values.
190 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800191
Austin Schuh0b406bb2015-02-17 02:28:48 -0800192 // Zeroing constants for wrist.
193 {kZeroingSampleSize, kClawEncoderIndexDifference,
194 0.9104180000000001},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800195
Austin Schuh0b406bb2015-02-17 02:28:48 -0800196 6.308141,
197 kClawPistonSwitchTime,
198 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000199
Austin Schuh0b406bb2015-02-17 02:28:48 -0800200 {// Elevator values, in meters.
201 // 0 is the portion of the elevator carriage that Spencer removed
202 // lining up with the bolt.
203 // Positive is up.
204 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000205
Austin Schuh0b406bb2015-02-17 02:28:48 -0800206 // Arm values, in radians.
207 // 0 is sticking straight out horizontally over the intake/front.
208 // Positive is rotating up and into the robot (towards the back).
209 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800210
Austin Schuh0b406bb2015-02-17 02:28:48 -0800211 // Elevator zeroing constants: left, right.
212 // TODO(sensors): Get actual offsets for these.
213 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.058672},
214 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.078646},
215 // Arm zeroing constants: left, right.
216 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.324437},
217 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.064683},
218 0.722230,
219 -0.081354,
220 -3.509611 - 0.007415 - -0.019081,
221 3.506927 - 0.170017 - -0.147970,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800222
223 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800224 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000225 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800226
227 kMaxAllowedLeftRightArmDifference,
228 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800229 };
Brian Silverman431500a2013-10-28 19:50:15 -0700230 break;
231 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800232 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800233 kDrivetrainEncoderRatio,
234 kArmEncoderRatio,
235 kArmPotRatio,
236 kElevatorEncoderRatio,
237 kElevatorPotRatio,
238 kElevatorGearboxOutputRadianDistance,
239 kClawEncoderRatio,
240 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800241 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800242 kLowGearRatio,
243 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800244 kPracticeLeftDriveShifter,
245 kPracticeRightDriveShifter,
246 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700247 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800248 control_loops::MakeVelocityDrivetrainLoop,
249 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000250 0.02, // drivetrain done delta
251 5.0, // drivetrain max speed
252
Comran Morshed79bd3db2015-02-07 14:51:13 +0000253 // Motion ranges: hard_lower_limit, hard_upper_limit,
254 // soft_lower_limit, soft_upper_limit
255 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuhbfb8b242015-02-16 15:45:22 -0800256 {// Claw values, in radians.
257 // 0 is level with the ground.
258 // Positive moves in the direction of positive encoder values.
259 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800260
Austin Schuhbfb8b242015-02-16 15:45:22 -0800261 // Zeroing constants for wrist.
262 // TODO(sensors): Get actual offsets for these.
Austin Schuh8a436e82015-02-16 23:31:28 -0800263 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.973311},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800264 6.1663463999999992,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800265
266 kClawPistonSwitchTime,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800267 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000268
Austin Schuhbfb8b242015-02-16 15:45:22 -0800269 {// Elevator values, in meters.
270 // 0 is at the top of the elevator frame.
271 // Positive is down towards the drivebase.
272 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000273
Austin Schuhbfb8b242015-02-16 15:45:22 -0800274 // Arm values, in radians.
275 // 0 is sticking straight out horizontally over the intake/front.
276 // Positive is rotating up and into the robot (towards the back).
277 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800278
Austin Schuhbfb8b242015-02-16 15:45:22 -0800279 // Elevator zeroing constants: left, right.
280 // TODO(sensors): Get actual offsets for these.
Daniel Pettiee4fa802015-02-17 10:39:27 -0800281 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
282 0.016041 + 0.001290},
283 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
284 0.011367 + 0.003216},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800285 // Arm zeroing constants: left, right.
286 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.312677},
287 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.40855},
288 0.72069366666666679 - 0.026008,
289 -0.078959636363636357 - 0.024646,
290 -3.4952331578947375 + 0.011776,
291 3.5263507647058816 - 0.018921 + 0.006545,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800292
293 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800294 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000295 // TODO(sensors): End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800296
297 kMaxAllowedLeftRightArmDifference,
298 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800299 };
Brian Silverman431500a2013-10-28 19:50:15 -0700300 break;
301 default:
302 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
303 }
304}
305
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000306const Values *DoGetValues() {
307 uint16_t team = ::aos::network::GetTeamNumber();
308 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
309 return DoGetValuesForTeam(team);
310}
311
Brian Silverman431500a2013-10-28 19:50:15 -0700312} // namespace
313
314const Values &GetValues() {
315 static ::aos::Once<const Values> once(DoGetValues);
316 return *once.Get();
317}
318
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000319const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700320 static ::aos::Mutex mutex;
321 ::aos::MutexLocker locker(&mutex);
322
323 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
324 // race conditions.
325 static ::std::map<uint16_t, const Values *> values;
326
327 if (values.count(team_number) == 0) {
328 values[team_number] = DoGetValuesForTeam(team_number);
329#if __has_feature(address_sanitizer)
330 __lsan_ignore_object(values[team_number]);
331#endif
332 }
333 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000334}
335
Brian Silverman431500a2013-10-28 19:50:15 -0700336} // namespace constants
337} // namespace frc971