blob: 3beedfb9b50512ac861d27d11a68719c21371567 [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
Brian Silverman1a6590d2013-11-04 14:46:46 -080034const double kCompDrivetrainEncoderRatio =
Brian Silvermanf970f2c2014-03-22 19:34:30 -070035 (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/;
36const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0;
37const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0;
Brian Silverman431500a2013-10-28 19:50:15 -070038
Brian Silvermanf970f2c2014-03-22 19:34:30 -070039const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio;
40const double kPracticeLowGearRatio = kCompLowGearRatio;
41const double kPracticeHighGearRatio = kCompHighGearRatio;
Brian Silverman1a6590d2013-11-04 14:46:46 -080042
Brian Silvermane5db0c62014-03-13 15:53:18 -070043const ShifterHallEffect kCompRightDriveShifter{555, 657, 660, 560, 0.2, 0.7};
44const ShifterHallEffect kCompLeftDriveShifter{555, 660, 644, 552, 0.2, 0.7};
Brian Silverman6eb51f12013-11-02 14:39:01 -070045
Austin Schuh010eb812014-10-25 18:06:49 -070046const ShifterHallEffect kPracticeRightDriveShifter{2.95, 3.95, 3.95, 2.95, 0.2, 0.7};
47const ShifterHallEffect kPracticeLeftDriveShifter{2.95, 4.2, 3.95, 3.0, 0.2, 0.7};
Brian Silvermane5db0c62014-03-13 15:53:18 -070048
Comran Morshed79bd3db2015-02-07 14:51:13 +000049// TODO(sensors): Get actual robot width before turning on robot.
Brian Silvermanad9e0002014-04-13 14:55:57 -070050const double kRobotWidth = 25.0 / 100.0 * 2.54;
51
Brian Silverman6eb51f12013-11-02 14:39:01 -070052
Comran Morshed79bd3db2015-02-07 14:51:13 +000053// ///// Superstructure Constants
54
55// Gearing ratios of the pots and encoders for the elevator and arm.
56// Ratio is output shaft rotations per encoder/pot rotation
57const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0;
58// const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0;
59const double kElevatorEncoderRatio = 14.0 / 84.0;
60// const double kElevatorPotRatio = 1.0;
61const double kClawEncoderRatio = 18.0 / 72.0;
62// const double kClawPotRatio = 18.0/72.0;
63
64// Elevator gearbox pulley output constants.
65const int kElevatorGearboxOutputPulleyTeeth = 32; // 32 teeth
66const double kElevatorGearboxOutputPitch = 0.005; // 5 mm/tooth
67const double kElevatorGearboxOutputRotationDistance =
68 kElevatorGearboxOutputPulleyTeeth * kElevatorGearboxOutputPitch;
69
Austin Schuh703b8d42015-02-01 14:56:34 -080070const double kMaxAllowedLeftRightArmDifference = 0.01; // radians
71const double kMaxAllowedLeftRightElevatorDifference = 0.01; // meters
72
Comran Morshed79bd3db2015-02-07 14:51:13 +000073// Number of radians between each index pulse on the arm.
74const double kArmEncoderIndexDifference = 2 * M_PI * kArmEncoderRatio;
75
76// Number of meters betwen index pulses on the elevator.
77const double kElevatorEncoderIndexDifference =
78 kElevatorGearboxOutputRotationDistance * kElevatorEncoderRatio;
79
80const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
81
82const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070083
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000084const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070085 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080086 case 1: // for tests
87 return new Values{
88 kCompDrivetrainEncoderRatio,
89 kCompLowGearRatio,
90 kCompHighGearRatio,
91 kCompLeftDriveShifter,
92 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -080093 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -070094 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -080095 control_loops::MakeVelocityDrivetrainLoop,
96 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +000097 0.02, // drivetrain done delta
98 5.0, // drivetrain max speed
99
100 // Zeroing constants: left arm, right arm, left elev, right elev
101 {
102 kZeroingSampleSize, kArmEncoderIndexDifference,
103 // TODO(sensors): Get actual offsets before turning on robot.
104 0.0 /*index_offset_at_zero*/
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800105 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000106 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
107 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
108 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
109 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
110
111 // Motion ranges: hard_lower_limit, hard_upper_limit,
112 // soft_lower_limit, soft_upper_limit
113 // TODO(sensors): Get actual bounds before turning on robot.
114 {
115 // Claw values, in radians.
116 // 0 is level with the ground.
117 // Positive moves in the direction of positive encoder values.
118 {0.0000000000, 1.5700000000,
119 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800120 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000121
122 {
123 // Elevator values, in meters.
Austin Schuh703b8d42015-02-01 14:56:34 -0800124 // TODO(austin): Fix this. Positive is up.
Comran Morshed79bd3db2015-02-07 14:51:13 +0000125 // 0 is at the top of the elevator frame.
126 // Positive is down towards the drivebase.
127 {0.0000000000, 0.6790000000,
128 0.2000000000, 0.6000000000},
129
130 // Arm values, in radians.
131 // 0 is sticking straight out horizontally over the intake/front.
132 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800133 {-1.570000000, 1.5700000000,
134 -1.200000000, 1.2000000000}
135 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000136 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800137
138 kMaxAllowedLeftRightArmDifference,
139 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800140 };
141 break;
Brian Silverman431500a2013-10-28 19:50:15 -0700142 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800143 return new Values{
Austin Schuh288c8c32014-02-16 17:20:17 -0800144 kCompDrivetrainEncoderRatio,
145 kCompLowGearRatio,
146 kCompHighGearRatio,
147 kCompLeftDriveShifter,
148 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700149 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700150 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800151 control_loops::MakeVelocityDrivetrainLoop,
152 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000153 0.02, // drivetrain done delta
154 5.0, // drivetrain max speed
155
156 // Zeroing constants: left arm, right arm, left elev, right elev
157 {
158 kZeroingSampleSize, kArmEncoderIndexDifference,
159 // TODO(sensors): Get actual offsets before turning on robot.
160 0.0 /*index_offset_at_zero*/
Austin Schuh60c56662014-02-17 14:37:19 -0800161 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000162 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
163 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
164 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
165 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
166
167 // Motion ranges: hard_lower_limit, hard_upper_limit,
168 // soft_lower_limit, soft_upper_limit
169 // TODO(sensors): Get actual bounds before turning on robot.
170 {
171 // Claw values, in radians.
172 // 0 is level with the ground.
173 // Positive moves in the direction of positive encoder values.
174 {0.0000000000, 1.5700000000,
175 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800176 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000177
178 {
179 // Elevator values, in meters.
180 // 0 is at the top of the elevator frame.
181 // Positive is down towards the drivebase.
182 {0.0000000000, 0.6790000000,
183 0.2000000000, 0.6000000000},
184
185 // Arm values, in radians.
186 // 0 is sticking straight out horizontally over the intake/front.
187 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800188 {-1.570000000, 1.5700000000,
189 -1.200000000, 1.2000000000}
190 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000191 // End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800192
193 kMaxAllowedLeftRightArmDifference,
194 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800195 };
Brian Silverman431500a2013-10-28 19:50:15 -0700196 break;
197 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800198 return new Values{
Austin Schuh288c8c32014-02-16 17:20:17 -0800199 kPracticeDrivetrainEncoderRatio,
200 kPracticeLowGearRatio,
201 kPracticeHighGearRatio,
202 kPracticeLeftDriveShifter,
203 kPracticeRightDriveShifter,
204 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700205 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800206 control_loops::MakeVelocityDrivetrainLoop,
207 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000208 0.02, // drivetrain done delta
209 5.0, // drivetrain max speed
210
211 // Zeroing constants: left arm, right arm, left elev, right elev
212 {
213 kZeroingSampleSize, kArmEncoderIndexDifference,
214 // TODO(sensors): Get actual offsets before turning on robot.
215 0.0 /*index_offset_at_zero*/
Austin Schuh60c56662014-02-17 14:37:19 -0800216 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000217 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
218 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
219 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
220 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
221 // TODO(sensors): End "sensors" values
222
223 // Motion ranges: hard_lower_limit, hard_upper_limit,
224 // soft_lower_limit, soft_upper_limit
225 // TODO(sensors): Get actual bounds before turning on robot.
226 {
227 // Claw values, in radians.
228 // 0 is level with the ground.
229 // Positive moves in the direction of positive encoder values.
230 {0.0000000000, 1.5700000000,
231 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800232 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000233
234 {
235 // Elevator values, in meters.
236 // 0 is at the top of the elevator frame.
237 // Positive is down towards the drivebase.
238 {0.0000000000, 0.6790000000,
239 0.2000000000, 0.6000000000},
240
241 // Arm values, in radians.
242 // 0 is sticking straight out horizontally over the intake/front.
243 // Positive is rotating up and into the robot (towards the back).
Austin Schuh703b8d42015-02-01 14:56:34 -0800244 {-1.570000000, 1.5700000000,
245 -1.200000000, 1.2000000000}
246 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000247 // TODO(sensors): End "sensor" values.
Austin Schuh703b8d42015-02-01 14:56:34 -0800248
249 kMaxAllowedLeftRightArmDifference,
250 kMaxAllowedLeftRightElevatorDifference,
Brian Silverman756f9ff2014-01-17 23:40:23 -0800251 };
Brian Silverman431500a2013-10-28 19:50:15 -0700252 break;
253 default:
254 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
255 }
256}
257
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000258const Values *DoGetValues() {
259 uint16_t team = ::aos::network::GetTeamNumber();
260 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
261 return DoGetValuesForTeam(team);
262}
263
Brian Silverman431500a2013-10-28 19:50:15 -0700264} // namespace
265
266const Values &GetValues() {
267 static ::aos::Once<const Values> once(DoGetValues);
268 return *once.Get();
269}
270
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000271const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700272 static ::aos::Mutex mutex;
273 ::aos::MutexLocker locker(&mutex);
274
275 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
276 // race conditions.
277 static ::std::map<uint16_t, const Values *> values;
278
279 if (values.count(team_number) == 0) {
280 values[team_number] = DoGetValuesForTeam(team_number);
281#if __has_feature(address_sanitizer)
282 __lsan_ignore_object(values[team_number]);
283#endif
284 }
285 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000286}
287
Brian Silverman431500a2013-10-28 19:50:15 -0700288} // namespace constants
289} // namespace frc971