blob: 3f5213f59170721c9b165e7857cf4ac1597353d3 [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.
Austin Schuh58baa352015-02-20 22:08:26 -080035const double kDrivetrainEncoderRatio = 20.0 / 50.0;
Daniel Pettia7827412015-02-13 20:55:57 -080036const 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
Daniel Pettiee4fa802015-02-17 10:39:27 -0800118 {// Claw values, in radians.
119 // 0 is level with the ground.
120 // Positive moves in the direction of positive encoder values.
Brian Silverman24e37ad2015-02-18 01:58:08 -0500121 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800122
Daniel Pettiee4fa802015-02-17 10:39:27 -0800123 // Zeroing constants for wrist.
Brian Silverman24e37ad2015-02-18 01:58:08 -0500124 {kZeroingSampleSize, kClawEncoderIndexDifference,
125 0.9},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800126
Brian Silverman24e37ad2015-02-18 01:58:08 -0500127 6.308141,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800128 kClawPistonSwitchTime,
129 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000130
Daniel Pettiee4fa802015-02-17 10:39:27 -0800131 {// Elevator values, in meters.
Brian Silverman24e37ad2015-02-18 01:58:08 -0500132 // 0 is the portion of the elevator carriage that Spencer removed
133 // lining up with the bolt.
134 // Positive is up.
135 {-0.005, 0.679000, 0.010000, 0.650000},
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).
Brian Silverman24e37ad2015-02-18 01:58:08 -0500140 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800141
Daniel Pettiee4fa802015-02-17 10:39:27 -0800142 // Elevator zeroing constants: left, right.
Daniel Pettiee4fa802015-02-17 10:39:27 -0800143 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
144 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
145 // Arm zeroing constants: left, right.
146 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
147 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
Brian Silverman24e37ad2015-02-18 01:58:08 -0500148 0.7,
149 -0.08,
150 -3.5 - 0.01 - -0.02,
151 3.5 - 0.17 - -0.15,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800152
Daniel Pettiee4fa802015-02-17 10:39:27 -0800153 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;
Austin Schuh58baa352015-02-20 22:08:26 -0800161 case kPracticeTeamNumber:
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,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800171 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800172 kLowGearRatio,
173 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800174 kCompLeftDriveShifter,
175 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700176 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700177 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800178 control_loops::MakeVelocityDrivetrainLoop,
179 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000180 0.02, // drivetrain done delta
181 5.0, // drivetrain max speed
182
Comran Morshed79bd3db2015-02-07 14:51:13 +0000183 // Motion ranges: hard_lower_limit, hard_upper_limit,
184 // soft_lower_limit, soft_upper_limit
185 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuh0b406bb2015-02-17 02:28:48 -0800186 {// Claw values, in radians.
187 // 0 is level with the ground.
188 // Positive moves in the direction of positive encoder values.
189 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800190
Austin Schuh0b406bb2015-02-17 02:28:48 -0800191 // Zeroing constants for wrist.
192 {kZeroingSampleSize, kClawEncoderIndexDifference,
193 0.9104180000000001},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800194
Austin Schuh0b406bb2015-02-17 02:28:48 -0800195 6.308141,
196 kClawPistonSwitchTime,
197 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000198
Austin Schuh0b406bb2015-02-17 02:28:48 -0800199 {// Elevator values, in meters.
200 // 0 is the portion of the elevator carriage that Spencer removed
201 // lining up with the bolt.
202 // Positive is up.
203 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000204
Austin Schuh0b406bb2015-02-17 02:28:48 -0800205 // Arm values, in radians.
206 // 0 is sticking straight out horizontally over the intake/front.
207 // Positive is rotating up and into the robot (towards the back).
208 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800209
Austin Schuh0b406bb2015-02-17 02:28:48 -0800210 // Elevator zeroing constants: left, right.
211 // TODO(sensors): Get actual offsets for these.
212 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.058672},
213 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.078646},
214 // Arm zeroing constants: left, right.
215 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.324437},
216 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.064683},
217 0.722230,
218 -0.081354,
219 -3.509611 - 0.007415 - -0.019081,
220 3.506927 - 0.170017 - -0.147970,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800221
222 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800223 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000224 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800225
226 kMaxAllowedLeftRightArmDifference,
227 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800228 };
Brian Silverman431500a2013-10-28 19:50:15 -0700229 break;
Austin Schuh58baa352015-02-20 22:08:26 -0800230 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800231 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800232 kDrivetrainEncoderRatio,
233 kArmEncoderRatio,
234 kArmPotRatio,
235 kElevatorEncoderRatio,
236 kElevatorPotRatio,
237 kElevatorGearboxOutputRadianDistance,
238 kClawEncoderRatio,
239 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800240 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800241 kLowGearRatio,
242 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800243 kPracticeLeftDriveShifter,
244 kPracticeRightDriveShifter,
245 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700246 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800247 control_loops::MakeVelocityDrivetrainLoop,
248 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000249 0.02, // drivetrain done delta
250 5.0, // drivetrain max speed
251
Comran Morshed79bd3db2015-02-07 14:51:13 +0000252 // Motion ranges: hard_lower_limit, hard_upper_limit,
253 // soft_lower_limit, soft_upper_limit
254 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuhbfb8b242015-02-16 15:45:22 -0800255 {// Claw values, in radians.
256 // 0 is level with the ground.
257 // Positive moves in the direction of positive encoder values.
258 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800259
Austin Schuhbfb8b242015-02-16 15:45:22 -0800260 // Zeroing constants for wrist.
261 // TODO(sensors): Get actual offsets for these.
Austin Schuh8a436e82015-02-16 23:31:28 -0800262 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.973311},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800263 6.1663463999999992,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800264
265 kClawPistonSwitchTime,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800266 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000267
Austin Schuhbfb8b242015-02-16 15:45:22 -0800268 {// Elevator values, in meters.
269 // 0 is at the top of the elevator frame.
270 // Positive is down towards the drivebase.
271 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000272
Austin Schuhbfb8b242015-02-16 15:45:22 -0800273 // Arm values, in radians.
274 // 0 is sticking straight out horizontally over the intake/front.
275 // Positive is rotating up and into the robot (towards the back).
276 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800277
Austin Schuhbfb8b242015-02-16 15:45:22 -0800278 // Elevator zeroing constants: left, right.
279 // TODO(sensors): Get actual offsets for these.
Daniel Pettiee4fa802015-02-17 10:39:27 -0800280 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
281 0.016041 + 0.001290},
282 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
283 0.011367 + 0.003216},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800284 // Arm zeroing constants: left, right.
Austin Schuh58baa352015-02-20 22:08:26 -0800285 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.060592},
286 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.210155},
287
Austin Schuhbfb8b242015-02-16 15:45:22 -0800288 0.72069366666666679 - 0.026008,
289 -0.078959636363636357 - 0.024646,
Austin Schuh58baa352015-02-20 22:08:26 -0800290 -3.509611 - 0.007415 - -0.019081 - 0.029393 - -0.013585,
291 3.506927 - 0.170017 - -0.147970 - 0.005045 - -0.026504,
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