blob: 13775b9368fe0d71305be42ce35bf159328ad548 [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};
Austin Schuhd8b2a242015-02-22 21:46:53 -080046const double kToteHeight = 0.3;
Brian Silvermane5db0c62014-03-13 15:53:18 -070047
Daniel Pettia7827412015-02-13 20:55:57 -080048// Set by Daniel on 2/13/15.
49// Distance from the center of the left wheel to the center of the right wheel.
50const double kRobotWidth = 37.806 /*inches*/ * 0.0254;
Brian Silverman6eb51f12013-11-02 14:39:01 -070051
Comran Morshed79bd3db2015-02-07 14:51:13 +000052// ///// Superstructure Constants
53
Comran Morshed79bd3db2015-02-07 14:51:13 +000054// Elevator gearbox pulley output constants.
55const int kElevatorGearboxOutputPulleyTeeth = 32; // 32 teeth
56const double kElevatorGearboxOutputPitch = 0.005; // 5 mm/tooth
Daniel Pettia7827412015-02-13 20:55:57 -080057const double kElevatorGearboxOutputRadianDistance =
58 kElevatorGearboxOutputPulleyTeeth * kElevatorGearboxOutputPitch /
59 (2.0 * M_PI);
Comran Morshed79bd3db2015-02-07 14:51:13 +000060
Daniel Pettie1bb13e2015-02-17 13:59:15 -080061const double kArmZeroingHeight = 0.2;
62
Daniel Pettiee4fa802015-02-17 10:39:27 -080063const double kMaxAllowedLeftRightArmDifference = 0.04; // radians
Austin Schuh703b8d42015-02-01 14:56:34 -080064const double kMaxAllowedLeftRightElevatorDifference = 0.01; // meters
65
Daniel Pettia7827412015-02-13 20:55:57 -080066// Gearing ratios of the pots and encoders for the elevator and arm.
67// Ratio is output shaft rotations per encoder/pot rotation
68// Checked by Daniel on 2/13/15.
69const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
70const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
71const double kElevatorEncoderRatio = 14.0 / 84.0;
72const double kElevatorPotRatio = 1.0;
73const double kClawEncoderRatio = 18.0 / 72.0;
74const double kClawPotRatio = 18.0 / 72.0;
75
Comran Morshed79bd3db2015-02-07 14:51:13 +000076// Number of radians between each index pulse on the arm.
Daniel Pettiee4fa802015-02-17 10:39:27 -080077const double kArmEncoderIndexDifference = 2.0 * M_PI * kArmEncoderRatio;
Daniel Pettia7827412015-02-13 20:55:57 -080078// Number of meters between each index pulse on the elevator.
Comran Morshed79bd3db2015-02-07 14:51:13 +000079const double kElevatorEncoderIndexDifference =
Daniel Pettiee4fa802015-02-17 10:39:27 -080080 kElevatorEncoderRatio * 2.0 * M_PI * // radians
Daniel Pettia7827412015-02-13 20:55:57 -080081 kElevatorGearboxOutputRadianDistance;
82// Number of radians between index pulses on the claw.
Comran Morshed79bd3db2015-02-07 14:51:13 +000083const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
84
85const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070086
Daniel Petti9cf68c82015-02-14 14:57:17 -080087// TODO(danielp): All these values might need to change.
88const double kClawPistonSwitchTime = 0.4;
89const double kClawZeroingRange = 0.3;
90
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000091const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070092 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080093 case 1: // for tests
94 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -080095 kDrivetrainEncoderRatio,
96 kArmEncoderRatio,
97 kArmPotRatio,
98 kElevatorEncoderRatio,
99 kElevatorPotRatio,
100 kElevatorGearboxOutputRadianDistance,
101 kClawEncoderRatio,
102 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800103 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800104 kLowGearRatio,
105 kHighGearRatio,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800106 kCompLeftDriveShifter,
107 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -0800108 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700109 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -0800110 control_loops::MakeVelocityDrivetrainLoop,
111 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000112 0.02, // drivetrain done delta
113 5.0, // drivetrain max speed
114
Comran Morshed79bd3db2015-02-07 14:51:13 +0000115 // Motion ranges: hard_lower_limit, hard_upper_limit,
116 // soft_lower_limit, soft_upper_limit
Daniel Pettiee4fa802015-02-17 10:39:27 -0800117 {// Claw values, in radians.
118 // 0 is level with the ground.
119 // Positive moves in the direction of positive encoder values.
Brian Silverman24e37ad2015-02-18 01:58:08 -0500120 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800121
Daniel Pettiee4fa802015-02-17 10:39:27 -0800122 // Zeroing constants for wrist.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000123 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.9, 0.3},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800124
Brian Silverman24e37ad2015-02-18 01:58:08 -0500125 6.308141,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800126 kClawPistonSwitchTime,
127 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000128
Daniel Pettiee4fa802015-02-17 10:39:27 -0800129 {// Elevator values, in meters.
Brian Silverman24e37ad2015-02-18 01:58:08 -0500130 // 0 is the portion of the elevator carriage that Spencer removed
131 // lining up with the bolt.
132 // Positive is up.
133 {-0.005, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000134
Daniel Pettiee4fa802015-02-17 10:39:27 -0800135 // Arm values, in radians.
136 // 0 is sticking straight out horizontally over the intake/front.
137 // Positive is rotating up and into the robot (towards the back).
Brian Silverman24e37ad2015-02-18 01:58:08 -0500138 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800139
Daniel Pettiee4fa802015-02-17 10:39:27 -0800140 // Elevator zeroing constants: left, right.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000141 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0, 0.3},
142 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0, 0.3},
Daniel Pettiee4fa802015-02-17 10:39:27 -0800143 // Arm zeroing constants: left, right.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000144 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0, 0.3},
145 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0, 0.3},
Brian Silverman24e37ad2015-02-18 01:58:08 -0500146 0.7,
147 -0.08,
148 -3.5 - 0.01 - -0.02,
149 3.5 - 0.17 - -0.15,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800150
Daniel Pettiee4fa802015-02-17 10:39:27 -0800151 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800152 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000153 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800154
155 kMaxAllowedLeftRightArmDifference,
156 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800157 };
158 break;
Austin Schuh58baa352015-02-20 22:08:26 -0800159 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800160 return new Values{
Daniel Pettia7827412015-02-13 20:55:57 -0800161 kDrivetrainEncoderRatio,
162 kArmEncoderRatio,
163 kArmPotRatio,
164 kElevatorEncoderRatio,
165 kElevatorPotRatio,
166 kElevatorGearboxOutputRadianDistance,
167 kClawEncoderRatio,
168 kClawPotRatio,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800169 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800170 kLowGearRatio,
171 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800172 kCompLeftDriveShifter,
173 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700174 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700175 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800176 control_loops::MakeVelocityDrivetrainLoop,
177 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000178 0.02, // drivetrain done delta
179 5.0, // drivetrain max speed
180
Comran Morshed79bd3db2015-02-07 14:51:13 +0000181 // Motion ranges: hard_lower_limit, hard_upper_limit,
182 // soft_lower_limit, soft_upper_limit
183 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuh0b406bb2015-02-17 02:28:48 -0800184 {// Claw values, in radians.
185 // 0 is level with the ground.
186 // Positive moves in the direction of positive encoder values.
187 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800188
Austin Schuh0b406bb2015-02-17 02:28:48 -0800189 // Zeroing constants for wrist.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000190 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.9104180000000001,
191 0.3},
Daniel Petti9cf68c82015-02-14 14:57:17 -0800192
Austin Schuh0b406bb2015-02-17 02:28:48 -0800193 6.308141,
194 kClawPistonSwitchTime,
195 kClawZeroingRange},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000196
Austin Schuh0b406bb2015-02-17 02:28:48 -0800197 {// Elevator values, in meters.
198 // 0 is the portion of the elevator carriage that Spencer removed
199 // lining up with the bolt.
200 // Positive is up.
201 {-0.00500, 0.679000, 0.010000, 0.650000},
Comran Morshed79bd3db2015-02-07 14:51:13 +0000202
Austin Schuh0b406bb2015-02-17 02:28:48 -0800203 // 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).
206 {-M_PI / 2 - 0.05, M_PI / 2 + 0.05, -M_PI / 2, M_PI / 2},
Daniel Pettia7827412015-02-13 20:55:57 -0800207
Austin Schuh0b406bb2015-02-17 02:28:48 -0800208 // Elevator zeroing constants: left, right.
209 // TODO(sensors): Get actual offsets for these.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000210 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.066380, 0.3},
211 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.051888, 0.3},
Austin Schuh0b406bb2015-02-17 02:28:48 -0800212 // Arm zeroing constants: left, right.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000213 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.324437, 0.3},
214 {kZeroingSampleSize, kArmEncoderIndexDifference, -0.064683, 0.3},
Austin Schuhd8b2a242015-02-22 21:46:53 -0800215 0.722230 - -0.000594,
216 -0.081354 - -0.000374,
Austin Schuh0b406bb2015-02-17 02:28:48 -0800217 -3.509611 - 0.007415 - -0.019081,
218 3.506927 - 0.170017 - -0.147970,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800219
220 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;
Austin Schuh58baa352015-02-20 22:08:26 -0800228 case kCompTeamNumber:
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,
Daniel Pettidfc90ba2015-02-17 21:42:15 -0800238 kToteHeight,
Daniel Pettia7827412015-02-13 20:55:57 -0800239 kLowGearRatio,
240 kHighGearRatio,
Austin Schuh288c8c32014-02-16 17:20:17 -0800241 kPracticeLeftDriveShifter,
242 kPracticeRightDriveShifter,
243 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700244 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800245 control_loops::MakeVelocityDrivetrainLoop,
246 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000247 0.02, // drivetrain done delta
248 5.0, // drivetrain max speed
249
Comran Morshed79bd3db2015-02-07 14:51:13 +0000250 // Motion ranges: hard_lower_limit, hard_upper_limit,
251 // soft_lower_limit, soft_upper_limit
252 // TODO(sensors): Get actual bounds before turning on robot.
Austin Schuhbfb8b242015-02-16 15:45:22 -0800253 {// Claw values, in radians.
254 // 0 is level with the ground.
255 // Positive moves in the direction of positive encoder values.
256 {-0.05, M_PI / 2.0 + 0.1, 0.0, M_PI / 2.0},
Daniel Pettia7827412015-02-13 20:55:57 -0800257
Austin Schuhbfb8b242015-02-16 15:45:22 -0800258 // Zeroing constants for wrist.
259 // TODO(sensors): Get actual offsets for these.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000260 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.973311, 0.3},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800261 6.1663463999999992,
Daniel Petti9cf68c82015-02-14 14:57:17 -0800262
263 kClawPistonSwitchTime,
Daniel Pettiee4fa802015-02-17 10:39:27 -0800264 kClawZeroingRange},
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.
Daniel Pettiee4fa802015-02-17 10:39:27 -0800278 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
Adam Snaider3cd11c52015-02-16 02:16:09 +0000279 0.016041 + 0.001290, 0.3},
Daniel Pettiee4fa802015-02-17 10:39:27 -0800280 {kZeroingSampleSize, kElevatorEncoderIndexDifference,
Adam Snaider3cd11c52015-02-16 02:16:09 +0000281 0.011367 + 0.003216, 0.3},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800282 // Arm zeroing constants: left, right.
Adam Snaider3cd11c52015-02-16 02:16:09 +0000283 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.060592, 0.3},
284 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.210155, 0.3},
Austin Schuhbfb8b242015-02-16 15:45:22 -0800285 0.72069366666666679 - 0.026008,
286 -0.078959636363636357 - 0.024646,
Austin Schuh58baa352015-02-20 22:08:26 -0800287 -3.509611 - 0.007415 - -0.019081 - 0.029393 - -0.013585,
288 3.506927 - 0.170017 - -0.147970 - 0.005045 - -0.026504,
Daniel Pettie1bb13e2015-02-17 13:59:15 -0800289
290 kArmZeroingHeight,
Austin Schuh703b8d42015-02-01 14:56:34 -0800291 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000292 // TODO(sensors): End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800293
294 kMaxAllowedLeftRightArmDifference,
295 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800296 };
Brian Silverman431500a2013-10-28 19:50:15 -0700297 break;
298 default:
299 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
300 }
301}
302
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000303const Values *DoGetValues() {
304 uint16_t team = ::aos::network::GetTeamNumber();
305 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
306 return DoGetValuesForTeam(team);
307}
308
Brian Silverman431500a2013-10-28 19:50:15 -0700309} // namespace
310
311const Values &GetValues() {
312 static ::aos::Once<const Values> once(DoGetValues);
313 return *once.Get();
314}
315
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000316const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700317 static ::aos::Mutex mutex;
318 ::aos::MutexLocker locker(&mutex);
319
320 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
321 // race conditions.
322 static ::std::map<uint16_t, const Values *> values;
323
324 if (values.count(team_number) == 0) {
325 values[team_number] = DoGetValuesForTeam(team_number);
326#if __has_feature(address_sanitizer)
327 __lsan_ignore_object(values[team_number]);
328#endif
329 }
330 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000331}
332
Brian Silverman431500a2013-10-28 19:50:15 -0700333} // namespace constants
334} // namespace frc971