blob: 696d9411bcf61ded84a1a76e4aa336c65c749902 [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
70// Number of radians between each index pulse on the arm.
71const double kArmEncoderIndexDifference = 2 * M_PI * kArmEncoderRatio;
72
73// Number of meters betwen index pulses on the elevator.
74const double kElevatorEncoderIndexDifference =
75 kElevatorGearboxOutputRotationDistance * kElevatorEncoderRatio;
76
77const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio;
78
79const int kZeroingSampleSize = 20;
Brian Silvermanedcfd2d2014-04-03 13:04:16 -070080
Ben Fredricksona9dcfa42014-02-23 02:05:59 +000081const Values *DoGetValuesForTeam(uint16_t team) {
Brian Silverman431500a2013-10-28 19:50:15 -070082 switch (team) {
Brian Silverman0d2b7cb2014-02-18 20:25:57 -080083 case 1: // for tests
84 return new Values{
85 kCompDrivetrainEncoderRatio,
86 kCompLowGearRatio,
87 kCompHighGearRatio,
88 kCompLeftDriveShifter,
89 kCompRightDriveShifter,
Austin Schuh11726212014-03-02 14:01:02 -080090 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -070091 0.5,
Austin Schuha25a0412014-03-09 00:50:04 -080092 control_loops::MakeVelocityDrivetrainLoop,
93 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +000094 0.02, // drivetrain done delta
95 5.0, // drivetrain max speed
96
97 // Zeroing constants: left arm, right arm, left elev, right elev
98 {
99 kZeroingSampleSize, kArmEncoderIndexDifference,
100 // TODO(sensors): Get actual offsets before turning on robot.
101 0.0 /*index_offset_at_zero*/
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800102 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000103 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
104 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
105 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
106 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
107
108 // Motion ranges: hard_lower_limit, hard_upper_limit,
109 // soft_lower_limit, soft_upper_limit
110 // TODO(sensors): Get actual bounds before turning on robot.
111 {
112 // Claw values, in radians.
113 // 0 is level with the ground.
114 // Positive moves in the direction of positive encoder values.
115 {0.0000000000, 1.5700000000,
116 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800117 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000118
119 {
120 // Elevator values, in meters.
121 // 0 is at the top of the elevator frame.
122 // Positive is down towards the drivebase.
123 {0.0000000000, 0.6790000000,
124 0.2000000000, 0.6000000000},
125
126 // Arm values, in radians.
127 // 0 is sticking straight out horizontally over the intake/front.
128 // Positive is rotating up and into the robot (towards the back).
129 {0.0000000000, 1.5700000000,
130 0.1000000000, 1.2000000000}
131 }
132 // End "sensor" values.
Brian Silverman0d2b7cb2014-02-18 20:25:57 -0800133 };
134 break;
Brian Silverman431500a2013-10-28 19:50:15 -0700135 case kCompTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800136 return new Values{
Austin Schuh288c8c32014-02-16 17:20:17 -0800137 kCompDrivetrainEncoderRatio,
138 kCompLowGearRatio,
139 kCompHighGearRatio,
140 kCompLeftDriveShifter,
141 kCompRightDriveShifter,
Brian Silvermanedcfd2d2014-04-03 13:04:16 -0700142 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700143 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800144 control_loops::MakeVelocityDrivetrainLoop,
145 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000146 0.02, // drivetrain done delta
147 5.0, // drivetrain max speed
148
149 // Zeroing constants: left arm, right arm, left elev, right elev
150 {
151 kZeroingSampleSize, kArmEncoderIndexDifference,
152 // TODO(sensors): Get actual offsets before turning on robot.
153 0.0 /*index_offset_at_zero*/
Austin Schuh60c56662014-02-17 14:37:19 -0800154 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000155 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
156 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
157 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
158 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
159
160 // Motion ranges: hard_lower_limit, hard_upper_limit,
161 // soft_lower_limit, soft_upper_limit
162 // TODO(sensors): Get actual bounds before turning on robot.
163 {
164 // Claw values, in radians.
165 // 0 is level with the ground.
166 // Positive moves in the direction of positive encoder values.
167 {0.0000000000, 1.5700000000,
168 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800169 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000170
171 {
172 // Elevator values, in meters.
173 // 0 is at the top of the elevator frame.
174 // Positive is down towards the drivebase.
175 {0.0000000000, 0.6790000000,
176 0.2000000000, 0.6000000000},
177
178 // Arm values, in radians.
179 // 0 is sticking straight out horizontally over the intake/front.
180 // Positive is rotating up and into the robot (towards the back).
181 {0.0000000000, 1.5700000000,
182 0.1000000000, 1.2000000000}
183 }
184 // End "sensor" values.
Brian Silverman756f9ff2014-01-17 23:40:23 -0800185 };
Brian Silverman431500a2013-10-28 19:50:15 -0700186 break;
187 case kPracticeTeamNumber:
Brian Silverman756f9ff2014-01-17 23:40:23 -0800188 return new Values{
Austin Schuh288c8c32014-02-16 17:20:17 -0800189 kPracticeDrivetrainEncoderRatio,
190 kPracticeLowGearRatio,
191 kPracticeHighGearRatio,
192 kPracticeLeftDriveShifter,
193 kPracticeRightDriveShifter,
194 false,
Brian Silvermanad9e0002014-04-13 14:55:57 -0700195 kRobotWidth,
Austin Schuha25a0412014-03-09 00:50:04 -0800196 control_loops::MakeVelocityDrivetrainLoop,
197 control_loops::MakeDrivetrainLoop,
Comran Morshed79bd3db2015-02-07 14:51:13 +0000198 0.02, // drivetrain done delta
199 5.0, // drivetrain max speed
200
201 // Zeroing constants: left arm, right arm, left elev, right elev
202 {
203 kZeroingSampleSize, kArmEncoderIndexDifference,
204 // TODO(sensors): Get actual offsets before turning on robot.
205 0.0 /*index_offset_at_zero*/
Austin Schuh60c56662014-02-17 14:37:19 -0800206 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000207 {kZeroingSampleSize, kArmEncoderIndexDifference, 0.0},
208 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
209 {kZeroingSampleSize, kElevatorEncoderIndexDifference, 0.0},
210 {kZeroingSampleSize, kClawEncoderIndexDifference, 0.0},
211 // TODO(sensors): End "sensors" values
212
213 // Motion ranges: hard_lower_limit, hard_upper_limit,
214 // soft_lower_limit, soft_upper_limit
215 // TODO(sensors): Get actual bounds before turning on robot.
216 {
217 // Claw values, in radians.
218 // 0 is level with the ground.
219 // Positive moves in the direction of positive encoder values.
220 {0.0000000000, 1.5700000000,
221 0.1000000000, 1.2000000000}
James Kuszmaul9ead1de2014-02-28 21:24:39 -0800222 },
Comran Morshed79bd3db2015-02-07 14:51:13 +0000223
224 {
225 // Elevator values, in meters.
226 // 0 is at the top of the elevator frame.
227 // Positive is down towards the drivebase.
228 {0.0000000000, 0.6790000000,
229 0.2000000000, 0.6000000000},
230
231 // Arm values, in radians.
232 // 0 is sticking straight out horizontally over the intake/front.
233 // Positive is rotating up and into the robot (towards the back).
234 {0.0000000000, 1.5700000000,
235 0.1000000000, 1.2000000000}
236 }
237 // TODO(sensors): End "sensor" values.
Brian Silverman756f9ff2014-01-17 23:40:23 -0800238 };
Brian Silverman431500a2013-10-28 19:50:15 -0700239 break;
240 default:
241 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
242 }
243}
244
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000245const Values *DoGetValues() {
246 uint16_t team = ::aos::network::GetTeamNumber();
247 LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
248 return DoGetValuesForTeam(team);
249}
250
Brian Silverman431500a2013-10-28 19:50:15 -0700251} // namespace
252
253const Values &GetValues() {
254 static ::aos::Once<const Values> once(DoGetValues);
255 return *once.Get();
256}
257
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000258const Values &GetValuesForTeam(uint16_t team_number) {
Brian Silverman0a151c92014-05-02 15:28:44 -0700259 static ::aos::Mutex mutex;
260 ::aos::MutexLocker locker(&mutex);
261
262 // IMPORTANT: This declaration has to stay after the mutex is locked to avoid
263 // race conditions.
264 static ::std::map<uint16_t, const Values *> values;
265
266 if (values.count(team_number) == 0) {
267 values[team_number] = DoGetValuesForTeam(team_number);
268#if __has_feature(address_sanitizer)
269 __lsan_ignore_object(values[team_number]);
270#endif
271 }
272 return *values[team_number];
Ben Fredricksona9dcfa42014-02-23 02:05:59 +0000273}
274
Brian Silverman431500a2013-10-28 19:50:15 -0700275} // namespace constants
276} // namespace frc971