Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 1 | #include "frc971/constants.h" |
| 2 | |
| 3 | #include <math.h> |
| 4 | #include <stdint.h> |
| 5 | #include <inttypes.h> |
| 6 | |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | |
| 9 | #if __has_feature(address_sanitizer) |
| 10 | #include "sanitizer/lsan_interface.h" |
| 11 | #endif |
| 12 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 13 | #include "aos/common/logging/logging.h" |
| 14 | #include "aos/common/once.h" |
| 15 | #include "aos/common/network/team_number.h" |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 16 | #include "aos/common/mutex.h" |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 17 | |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 18 | #include "frc971/control_loops/drivetrain/polydrivetrain_dog_motor_plant.h" |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 19 | #include "frc971/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 20 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 21 | #ifndef M_PI |
| 22 | #define M_PI 3.14159265358979323846 |
| 23 | #endif |
| 24 | |
| 25 | namespace frc971 { |
| 26 | namespace constants { |
| 27 | namespace { |
| 28 | |
Brian Silverman | e5db0c6 | 2014-03-13 15:53:18 -0700 | [diff] [blame] | 29 | const uint16_t kCompTeamNumber = 971; |
Brian Silverman | a20703b | 2014-03-20 14:29:37 -0700 | [diff] [blame] | 30 | const uint16_t kPracticeTeamNumber = 9971; |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 31 | |
| 32 | // ///// Drivetrain Constants |
Brian Silverman | e5db0c6 | 2014-03-13 15:53:18 -0700 | [diff] [blame] | 33 | |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 34 | const double kCompDrivetrainEncoderRatio = |
Brian Silverman | f970f2c | 2014-03-22 19:34:30 -0700 | [diff] [blame] | 35 | (18.0 / 50.0) /*output reduction*/ * (56.0 / 30.0) /*encoder gears*/; |
| 36 | const double kCompLowGearRatio = 18.0 / 60.0 * 18.0 / 50.0; |
| 37 | const double kCompHighGearRatio = 28.0 / 50.0 * 18.0 / 50.0; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 38 | |
Brian Silverman | f970f2c | 2014-03-22 19:34:30 -0700 | [diff] [blame] | 39 | const double kPracticeDrivetrainEncoderRatio = kCompDrivetrainEncoderRatio; |
| 40 | const double kPracticeLowGearRatio = kCompLowGearRatio; |
| 41 | const double kPracticeHighGearRatio = kCompHighGearRatio; |
Brian Silverman | 1a6590d | 2013-11-04 14:46:46 -0800 | [diff] [blame] | 42 | |
Brian Silverman | e5db0c6 | 2014-03-13 15:53:18 -0700 | [diff] [blame] | 43 | const ShifterHallEffect kCompRightDriveShifter{555, 657, 660, 560, 0.2, 0.7}; |
| 44 | const ShifterHallEffect kCompLeftDriveShifter{555, 660, 644, 552, 0.2, 0.7}; |
Brian Silverman | 6eb51f1 | 2013-11-02 14:39:01 -0700 | [diff] [blame] | 45 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 46 | const ShifterHallEffect kPracticeRightDriveShifter{2.95, 3.95, 3.95, 2.95, 0.2, 0.7}; |
| 47 | const ShifterHallEffect kPracticeLeftDriveShifter{2.95, 4.2, 3.95, 3.0, 0.2, 0.7}; |
Brian Silverman | e5db0c6 | 2014-03-13 15:53:18 -0700 | [diff] [blame] | 48 | |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 49 | // TODO(sensors): Get actual robot width before turning on robot. |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 50 | const double kRobotWidth = 25.0 / 100.0 * 2.54; |
| 51 | |
Brian Silverman | 6eb51f1 | 2013-11-02 14:39:01 -0700 | [diff] [blame] | 52 | |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 53 | // ///// 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 |
| 57 | const double kArmEncoderRatio = 18.0 / 48.0 * 16.0 / 72.0; |
| 58 | // const double kArmPotRatio = 48.0 / 48.0 * 16.0 / 72.0; |
| 59 | const double kElevatorEncoderRatio = 14.0 / 84.0; |
| 60 | // const double kElevatorPotRatio = 1.0; |
| 61 | const double kClawEncoderRatio = 18.0 / 72.0; |
| 62 | // const double kClawPotRatio = 18.0/72.0; |
| 63 | |
| 64 | // Elevator gearbox pulley output constants. |
| 65 | const int kElevatorGearboxOutputPulleyTeeth = 32; // 32 teeth |
| 66 | const double kElevatorGearboxOutputPitch = 0.005; // 5 mm/tooth |
| 67 | const double kElevatorGearboxOutputRotationDistance = |
| 68 | kElevatorGearboxOutputPulleyTeeth * kElevatorGearboxOutputPitch; |
| 69 | |
| 70 | // Number of radians between each index pulse on the arm. |
| 71 | const double kArmEncoderIndexDifference = 2 * M_PI * kArmEncoderRatio; |
| 72 | |
| 73 | // Number of meters betwen index pulses on the elevator. |
| 74 | const double kElevatorEncoderIndexDifference = |
| 75 | kElevatorGearboxOutputRotationDistance * kElevatorEncoderRatio; |
| 76 | |
| 77 | const double kClawEncoderIndexDifference = 2.0 * M_PI * kClawEncoderRatio; |
| 78 | |
| 79 | const int kZeroingSampleSize = 20; |
Brian Silverman | edcfd2d | 2014-04-03 13:04:16 -0700 | [diff] [blame] | 80 | |
Ben Fredrickson | a9dcfa4 | 2014-02-23 02:05:59 +0000 | [diff] [blame] | 81 | const Values *DoGetValuesForTeam(uint16_t team) { |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 82 | switch (team) { |
Brian Silverman | 0d2b7cb | 2014-02-18 20:25:57 -0800 | [diff] [blame] | 83 | case 1: // for tests |
| 84 | return new Values{ |
| 85 | kCompDrivetrainEncoderRatio, |
| 86 | kCompLowGearRatio, |
| 87 | kCompHighGearRatio, |
| 88 | kCompLeftDriveShifter, |
| 89 | kCompRightDriveShifter, |
Austin Schuh | 1172621 | 2014-03-02 14:01:02 -0800 | [diff] [blame] | 90 | false, |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 91 | 0.5, |
Austin Schuh | a25a041 | 2014-03-09 00:50:04 -0800 | [diff] [blame] | 92 | control_loops::MakeVelocityDrivetrainLoop, |
| 93 | control_loops::MakeDrivetrainLoop, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 94 | 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 Silverman | 0d2b7cb | 2014-02-18 20:25:57 -0800 | [diff] [blame] | 102 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 103 | {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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame] | 117 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 118 | |
| 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 Silverman | 0d2b7cb | 2014-02-18 20:25:57 -0800 | [diff] [blame] | 133 | }; |
| 134 | break; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 135 | case kCompTeamNumber: |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 136 | return new Values{ |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 137 | kCompDrivetrainEncoderRatio, |
| 138 | kCompLowGearRatio, |
| 139 | kCompHighGearRatio, |
| 140 | kCompLeftDriveShifter, |
| 141 | kCompRightDriveShifter, |
Brian Silverman | edcfd2d | 2014-04-03 13:04:16 -0700 | [diff] [blame] | 142 | false, |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 143 | kRobotWidth, |
Austin Schuh | a25a041 | 2014-03-09 00:50:04 -0800 | [diff] [blame] | 144 | control_loops::MakeVelocityDrivetrainLoop, |
| 145 | control_loops::MakeDrivetrainLoop, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 146 | 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 Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame] | 154 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 155 | {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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame] | 169 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 170 | |
| 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 Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 185 | }; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 186 | break; |
| 187 | case kPracticeTeamNumber: |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 188 | return new Values{ |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 189 | kPracticeDrivetrainEncoderRatio, |
| 190 | kPracticeLowGearRatio, |
| 191 | kPracticeHighGearRatio, |
| 192 | kPracticeLeftDriveShifter, |
| 193 | kPracticeRightDriveShifter, |
| 194 | false, |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 195 | kRobotWidth, |
Austin Schuh | a25a041 | 2014-03-09 00:50:04 -0800 | [diff] [blame] | 196 | control_loops::MakeVelocityDrivetrainLoop, |
| 197 | control_loops::MakeDrivetrainLoop, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 198 | 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 Schuh | 60c5666 | 2014-02-17 14:37:19 -0800 | [diff] [blame] | 206 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 207 | {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 Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame] | 222 | }, |
Comran Morshed | 79bd3db | 2015-02-07 14:51:13 +0000 | [diff] [blame] | 223 | |
| 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 Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 238 | }; |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 242 | } |
| 243 | } |
| 244 | |
Ben Fredrickson | a9dcfa4 | 2014-02-23 02:05:59 +0000 | [diff] [blame] | 245 | const 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 Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 251 | } // namespace |
| 252 | |
| 253 | const Values &GetValues() { |
| 254 | static ::aos::Once<const Values> once(DoGetValues); |
| 255 | return *once.Get(); |
| 256 | } |
| 257 | |
Ben Fredrickson | a9dcfa4 | 2014-02-23 02:05:59 +0000 | [diff] [blame] | 258 | const Values &GetValuesForTeam(uint16_t team_number) { |
Brian Silverman | 0a151c9 | 2014-05-02 15:28:44 -0700 | [diff] [blame] | 259 | 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 Fredrickson | a9dcfa4 | 2014-02-23 02:05:59 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Brian Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 275 | } // namespace constants |
| 276 | } // namespace frc971 |