Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/claw/claw.h" |
| 2 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 5 | #include "aos/common/controls/control_loops.q.h" |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 6 | #include "aos/common/logging/logging.h" |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 7 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 8 | #include "aos/common/logging/matrix_logging.h" |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 9 | |
| 10 | #include "frc971/constants.h" |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/claw/claw_motor_plant.h" |
| 12 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 13 | // Zeroing plan. |
| 14 | // There are 2 types of zeros. Enabled and disabled ones. |
| 15 | // Disabled ones are only valid during auto mode, and can be used to speed up |
| 16 | // the enabled zero process. We need to re-zero during teleop in case the auto |
| 17 | // zero was poor and causes us to miss all our shots. |
| 18 | // |
| 19 | // We need to be able to zero manually while disabled by moving the joint over |
| 20 | // the zeros. |
| 21 | // Zero on the down edge when disabled (gravity in the direction of motion) |
| 22 | // |
| 23 | // When enabled, zero on the up edge (gravity opposing the direction of motion) |
| 24 | // The enabled sequence needs to work as follows. We can crash the claw if we |
| 25 | // bring them too close to each other or too far from each other. The only safe |
| 26 | // thing to do is to move them in unison. |
| 27 | // |
| 28 | // Start by moving them both towards the front of the bot to either find either |
| 29 | // the middle hall effect on either jaw, or the front hall effect on the bottom |
| 30 | // jaw. Any edge that isn't the desired edge will provide an approximate edge |
| 31 | // location that can be used for the fine tuning step. |
| 32 | // Once an edge is found on the front claw, move back the other way with both |
| 33 | // claws until an edge is found for the other claw. |
| 34 | // Now that we have an approximate zero, we can robustify the limits to keep |
| 35 | // both claws safe. Then, we can move both claws to a position that is the |
| 36 | // correct side of the zero and go zero. |
| 37 | |
| 38 | // Valid region plan. |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 39 | // Difference between the arms has a range, and the values of each arm has a |
| 40 | // range. |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 41 | // If a claw runs up against a static limit, don't let the goal change outside |
| 42 | // the limit. |
| 43 | // If a claw runs up against a movable limit, move both claws outwards to get |
| 44 | // out of the condition. |
| 45 | |
| 46 | namespace frc971 { |
| 47 | namespace control_loops { |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | template <typename T> int sign(T val) { |
| 51 | if (val > T(0)) { |
| 52 | return 1; |
| 53 | } else { |
| 54 | return -1; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } // namespace |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 59 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 60 | static const double kZeroingVoltage = 4.0; |
| 61 | static const double kMaxVoltage = 12.0; |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 62 | const double kRezeroThreshold = 0.03; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 64 | ClawLimitedLoop::ClawLimitedLoop(StateFeedbackLoop<4, 2, 2> loop) |
| 65 | : StateFeedbackLoop<4, 2, 2>(loop), |
| 66 | uncapped_average_voltage_(0.0), |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 67 | is_zeroing_(true), |
| 68 | U_Poly_((Eigen::Matrix<double, 4, 2>() << 1, 0, |
| 69 | -1, 0, |
| 70 | 0, 1, |
| 71 | 0, -1).finished(), |
| 72 | (Eigen::Matrix<double, 4, 1>() << kMaxVoltage, kMaxVoltage, |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 73 | kMaxVoltage, kMaxVoltage).finished()), |
| 74 | U_Poly_zeroing_((Eigen::Matrix<double, 4, 2>() << 1, 0, |
| 75 | -1, 0, |
| 76 | 0, 1, |
| 77 | 0, -1).finished(), |
| 78 | (Eigen::Matrix<double, 4, 1>() << |
| 79 | kZeroingVoltage, kZeroingVoltage, |
| 80 | kZeroingVoltage, kZeroingVoltage).finished()) { |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 81 | ::aos::controls::HPolytope<0>::Init(); |
| 82 | } |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 83 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 84 | // Caps the voltage prioritizing reducing velocity error over reducing |
| 85 | // positional error. |
| 86 | // Uses the polytope libararies which we used to just use for the drivetrain. |
| 87 | // Uses a region representing the maximum voltage and then transforms it such |
| 88 | // that the points represent different amounts of positional error and |
| 89 | // constrains the region such that, if at all possible, it will maintain its |
| 90 | // current efforts to reduce velocity error. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 91 | void ClawLimitedLoop::CapU() { |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 92 | Eigen::Matrix<double, 4, 1> error = R - X_hat; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 93 | |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 94 | double u_top = U(1, 0); |
| 95 | double u_bottom = U(0, 0); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 96 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 97 | uncapped_average_voltage_ = (u_top + u_bottom) / 2; |
| 98 | |
| 99 | double max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage; |
| 100 | |
| 101 | if (::std::abs(u_bottom) > max_voltage or ::std::abs(u_top) > max_voltage) { |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 102 | LOG_MATRIX(DEBUG, "U at start", U); |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 103 | // H * U <= k |
| 104 | // U = UPos + UVel |
| 105 | // H * (UPos + UVel) <= k |
| 106 | // H * UPos <= k - H * UVel |
| 107 | |
| 108 | // Now, we can do a coordinate transformation and say the following. |
| 109 | |
| 110 | // UPos = position_K * position_error |
| 111 | // (H * position_K) * position_error <= k - H * UVel |
| 112 | |
| 113 | Eigen::Matrix<double, 2, 2> position_K; |
| 114 | position_K << K(0, 0), K(0, 1), |
| 115 | K(1, 0), K(1, 1); |
| 116 | Eigen::Matrix<double, 2, 2> velocity_K; |
| 117 | velocity_K << K(0, 2), K(0, 3), |
| 118 | K(1, 2), K(1, 3); |
| 119 | |
| 120 | Eigen::Matrix<double, 2, 1> position_error; |
| 121 | position_error << error(0, 0), error(1, 0); |
| 122 | Eigen::Matrix<double, 2, 1> velocity_error; |
| 123 | velocity_error << error(2, 0), error(3, 0); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 124 | LOG_MATRIX(DEBUG, "error", error); |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 125 | |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 126 | const auto &poly = is_zeroing_ ? U_Poly_zeroing_ : U_Poly_; |
| 127 | const Eigen::Matrix<double, 4, 2> pos_poly_H = poly.H() * position_K; |
| 128 | const Eigen::Matrix<double, 4, 1> pos_poly_k = |
| 129 | poly.k() - poly.H() * velocity_K * velocity_error; |
| 130 | const ::aos::controls::HPolytope<2> pos_poly(pos_poly_H, pos_poly_k); |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 131 | |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 132 | Eigen::Matrix<double, 2, 1> adjusted_pos_error; |
| 133 | { |
| 134 | const auto &P = position_error; |
Brian Silverman | b087c0a | 2014-03-30 12:59:52 -0700 | [diff] [blame] | 135 | |
| 136 | // This line was at 45 degrees but is now at some angle steeper than the |
| 137 | // straight one between the points. |
| 138 | Eigen::Matrix<double, 1, 2> angle_45; |
| 139 | // If the top claw is above its soft upper limit, make the line actually |
| 140 | // 45 degrees to avoid smashing it into the limit in an attempt to fix the |
| 141 | // separation error faster than the bottom position one. |
| 142 | if (X_hat(0, 0) + X_hat(1, 0) > |
| 143 | constants::GetValues().claw.upper_claw.upper_limit) { |
| 144 | angle_45 << 1, 1; |
| 145 | } else { |
| 146 | // Fixing separation error half as fast as positional error works well |
| 147 | // because it means they both close evenly. |
| 148 | angle_45 << ::std::sqrt(3), 1; |
| 149 | } |
| 150 | Eigen::Matrix<double, 1, 2> L45_quadrant; |
| 151 | L45_quadrant << sign(P(1, 0)), -sign(P(0, 0)); |
| 152 | const auto L45 = L45_quadrant.cwiseProduct(angle_45); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 153 | const double w45 = 0; |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 154 | |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 155 | Eigen::Matrix<double, 1, 2> LH; |
| 156 | if (::std::abs(P(0, 0)) > ::std::abs(P(1, 0))) { |
| 157 | LH << 0, 1; |
| 158 | } else { |
| 159 | LH << 1, 0; |
| 160 | } |
| 161 | const double wh = LH.dot(P); |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 162 | |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 163 | Eigen::Matrix<double, 2, 2> standard; |
| 164 | standard << L45, LH; |
| 165 | Eigen::Matrix<double, 2, 1> W; |
| 166 | W << w45, wh; |
| 167 | const Eigen::Matrix<double, 2, 1> intersection = standard.inverse() * W; |
| 168 | |
| 169 | bool is_inside_h; |
| 170 | const auto adjusted_pos_error_h = |
| 171 | DoCoerceGoal(pos_poly, LH, wh, position_error, &is_inside_h); |
| 172 | const auto adjusted_pos_error_45 = |
| 173 | DoCoerceGoal(pos_poly, L45, w45, intersection, nullptr); |
| 174 | if (pos_poly.IsInside(intersection)) { |
| 175 | adjusted_pos_error = adjusted_pos_error_h; |
| 176 | } else { |
| 177 | if (is_inside_h) { |
| 178 | if (adjusted_pos_error_h.norm() > adjusted_pos_error_45.norm()) { |
| 179 | adjusted_pos_error = adjusted_pos_error_h; |
| 180 | } else { |
| 181 | adjusted_pos_error = adjusted_pos_error_45; |
| 182 | } |
| 183 | } else { |
| 184 | adjusted_pos_error = adjusted_pos_error_45; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | LOG_MATRIX(DEBUG, "adjusted_pos_error", adjusted_pos_error); |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 190 | U = velocity_K * velocity_error + position_K * adjusted_pos_error; |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 191 | LOG_MATRIX(DEBUG, "U is now", U); |
Brian Silverman | 7b7c907 | 2014-03-30 13:33:30 -0700 | [diff] [blame] | 192 | |
| 193 | { |
| 194 | const auto values = constants::GetValues().claw; |
Brian Silverman | ed9df2f | 2014-04-05 07:07:15 -0700 | [diff] [blame] | 195 | if (top_known_) { |
| 196 | if (X_hat(0, 0) + X_hat(1, 0) > values.upper_claw.upper_limit && |
| 197 | U(1, 0) > 0) { |
| 198 | LOG(WARNING, "upper claw too high and moving up\n"); |
| 199 | U(1, 0) = 0; |
| 200 | } else if (X_hat(0, 0) + X_hat(1, 0) < values.upper_claw.lower_limit && |
| 201 | U(1, 0) < 0) { |
| 202 | LOG(WARNING, "upper claw too low and moving down\n"); |
| 203 | U(1, 0) = 0; |
| 204 | } |
Brian Silverman | 7b7c907 | 2014-03-30 13:33:30 -0700 | [diff] [blame] | 205 | } |
Brian Silverman | ed9df2f | 2014-04-05 07:07:15 -0700 | [diff] [blame] | 206 | if (bottom_known_) { |
| 207 | if (X_hat(0, 0) > values.lower_claw.upper_limit && U(0, 0) > 0) { |
| 208 | LOG(WARNING, "lower claw too high and moving up\n"); |
| 209 | U(0, 0) = 0; |
| 210 | } else if (X_hat(0, 0) < values.lower_claw.lower_limit && U(0, 0) < 0) { |
| 211 | LOG(WARNING, "lower claw too low and moving down\n"); |
| 212 | U(0, 0) = 0; |
| 213 | } |
Brian Silverman | 7b7c907 | 2014-03-30 13:33:30 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 216 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 219 | ZeroedStateFeedbackLoop::ZeroedStateFeedbackLoop(const char *name, |
| 220 | ClawMotor *motor) |
| 221 | : offset_(0.0), |
| 222 | name_(name), |
| 223 | motor_(motor), |
| 224 | zeroing_state_(UNKNOWN_POSITION), |
| 225 | posedge_value_(0.0), |
| 226 | negedge_value_(0.0), |
| 227 | encoder_(0.0), |
| 228 | last_encoder_(0.0) {} |
| 229 | |
| 230 | void ZeroedStateFeedbackLoop::SetPositionValues(const HalfClawPosition &claw) { |
| 231 | front_.Update(claw.front); |
| 232 | calibration_.Update(claw.calibration); |
| 233 | back_.Update(claw.back); |
| 234 | |
| 235 | bool any_sensor_triggered = any_triggered(); |
| 236 | if (any_sensor_triggered && any_triggered_last_) { |
| 237 | // We are still on the hall effect and nothing has changed. |
| 238 | min_hall_effect_on_angle_ = |
| 239 | ::std::min(min_hall_effect_on_angle_, claw.position); |
| 240 | max_hall_effect_on_angle_ = |
| 241 | ::std::max(max_hall_effect_on_angle_, claw.position); |
| 242 | } else if (!any_sensor_triggered && !any_triggered_last_) { |
| 243 | // We are still off the hall effect and nothing has changed. |
| 244 | min_hall_effect_off_angle_ = |
| 245 | ::std::min(min_hall_effect_off_angle_, claw.position); |
| 246 | max_hall_effect_off_angle_ = |
| 247 | ::std::max(max_hall_effect_off_angle_, claw.position); |
| 248 | } else if (any_sensor_triggered && !any_triggered_last_) { |
| 249 | // Saw a posedge on the hall effect. Reset the limits. |
| 250 | min_hall_effect_on_angle_ = ::std::min(claw.posedge_value, claw.position); |
| 251 | max_hall_effect_on_angle_ = ::std::max(claw.posedge_value, claw.position); |
| 252 | } else if (!any_sensor_triggered && any_triggered_last_) { |
| 253 | // Saw a negedge on the hall effect. Reset the limits. |
| 254 | min_hall_effect_off_angle_ = ::std::min(claw.negedge_value, claw.position); |
| 255 | max_hall_effect_off_angle_ = ::std::max(claw.negedge_value, claw.position); |
| 256 | } |
| 257 | |
| 258 | posedge_value_ = claw.posedge_value; |
| 259 | negedge_value_ = claw.negedge_value; |
| 260 | last_encoder_ = encoder_; |
| 261 | if (front().value() || calibration().value() || back().value()) { |
| 262 | last_on_encoder_ = encoder_; |
| 263 | } else { |
| 264 | last_off_encoder_ = encoder_; |
| 265 | } |
| 266 | encoder_ = claw.position; |
| 267 | any_triggered_last_ = any_sensor_triggered; |
| 268 | } |
| 269 | |
| 270 | void ZeroedStateFeedbackLoop::Reset(const HalfClawPosition &claw) { |
| 271 | set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION); |
| 272 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 273 | front_.Reset(claw.front); |
| 274 | calibration_.Reset(claw.calibration); |
| 275 | back_.Reset(claw.back); |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 276 | // close up the min and max edge positions as they are no longer valid and |
| 277 | // will be expanded in future iterations |
| 278 | min_hall_effect_on_angle_ = claw.position; |
| 279 | max_hall_effect_on_angle_ = claw.position; |
| 280 | min_hall_effect_off_angle_ = claw.position; |
| 281 | max_hall_effect_off_angle_ = claw.position; |
| 282 | any_triggered_last_ = any_triggered(); |
| 283 | } |
| 284 | |
| 285 | bool TopZeroedStateFeedbackLoop::SetCalibrationOnEdge( |
| 286 | const constants::Values::Claws::Claw &claw_values, |
| 287 | JointZeroingState zeroing_state) { |
| 288 | double edge_encoder; |
| 289 | double edge_angle; |
| 290 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 291 | LOG(INFO, "Calibration edge edge should be %f.\n", edge_angle); |
| 292 | SetCalibration(edge_encoder, edge_angle); |
| 293 | set_zeroing_state(zeroing_state); |
| 294 | return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 299 | void TopZeroedStateFeedbackLoop::HandleCalibrationError( |
| 300 | const constants::Values::Claws::Claw &claw_values) { |
| 301 | double edge_encoder; |
| 302 | double edge_angle; |
| 303 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 304 | const double calibration_error = |
| 305 | ComputeCalibrationChange(edge_encoder, edge_angle); |
| 306 | LOG(INFO, "Top calibration error is %f\n", calibration_error); |
| 307 | if (::std::abs(calibration_error) > kRezeroThreshold) { |
| 308 | SetCalibration(edge_encoder, edge_angle); |
| 309 | set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | void BottomZeroedStateFeedbackLoop::HandleCalibrationError( |
| 316 | const constants::Values::Claws::Claw &claw_values) { |
| 317 | double edge_encoder; |
| 318 | double edge_angle; |
| 319 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 320 | const double calibration_error = |
| 321 | ComputeCalibrationChange(edge_encoder, edge_angle); |
| 322 | LOG(INFO, "Bottom calibration error is %f\n", calibration_error); |
| 323 | if (::std::abs(calibration_error) > kRezeroThreshold) { |
| 324 | SetCalibration(edge_encoder, edge_angle); |
| 325 | set_zeroing_state(ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 330 | bool BottomZeroedStateFeedbackLoop::SetCalibrationOnEdge( |
| 331 | const constants::Values::Claws::Claw &claw_values, |
| 332 | JointZeroingState zeroing_state) { |
| 333 | double edge_encoder; |
| 334 | double edge_angle; |
| 335 | if (GetPositionOfEdge(claw_values, &edge_encoder, &edge_angle)) { |
| 336 | LOG(INFO, "Calibration edge.\n"); |
| 337 | SetCalibration(edge_encoder, edge_angle); |
| 338 | set_zeroing_state(zeroing_state); |
| 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 344 | ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw) |
Brian Silverman | 3811150 | 2014-04-10 12:36:26 -0700 | [diff] [blame^] | 345 | : aos::controls::ControlLoop<control_loops::ClawGroup, true, true, |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 346 | false>(my_claw), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 347 | has_top_claw_goal_(false), |
| 348 | top_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 349 | top_claw_(this), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 350 | has_bottom_claw_goal_(false), |
| 351 | bottom_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 352 | bottom_claw_(this), |
| 353 | claw_(MakeClawLoop()), |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 354 | was_enabled_(false), |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 355 | doing_calibration_fine_tune_(false), |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 356 | capped_goal_(false), |
| 357 | mode_(UNKNOWN_LOCATION) {} |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 358 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 359 | const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 360 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 361 | bool ZeroedStateFeedbackLoop::SawFilteredPosedge( |
| 362 | const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA, |
| 363 | const HallEffectTracker &sensorB) { |
| 364 | if (posedge_filter_ == nullptr && this_sensor.posedge_count_changed() && |
| 365 | !sensorA.posedge_count_changed() && !sensorB.posedge_count_changed() && |
| 366 | this_sensor.value() && !this_sensor.last_value()) { |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 367 | posedge_filter_ = &this_sensor; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 368 | } else if (posedge_filter_ == &this_sensor && |
| 369 | !this_sensor.posedge_count_changed() && |
| 370 | !sensorA.posedge_count_changed() && |
| 371 | !sensorB.posedge_count_changed() && this_sensor.value()) { |
| 372 | posedge_filter_ = nullptr; |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 373 | return true; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 374 | } else if (posedge_filter_ == &this_sensor) { |
| 375 | posedge_filter_ = nullptr; |
| 376 | } |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | bool ZeroedStateFeedbackLoop::SawFilteredNegedge( |
| 381 | const HallEffectTracker &this_sensor, const HallEffectTracker &sensorA, |
| 382 | const HallEffectTracker &sensorB) { |
| 383 | if (negedge_filter_ == nullptr && this_sensor.negedge_count_changed() && |
| 384 | !sensorA.negedge_count_changed() && !sensorB.negedge_count_changed() && |
| 385 | !this_sensor.value() && this_sensor.last_value()) { |
| 386 | negedge_filter_ = &this_sensor; |
| 387 | } else if (negedge_filter_ == &this_sensor && |
| 388 | !this_sensor.negedge_count_changed() && |
| 389 | !sensorA.negedge_count_changed() && |
| 390 | !sensorB.negedge_count_changed() && !this_sensor.value()) { |
| 391 | negedge_filter_ = nullptr; |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 392 | return true; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 393 | } else if (negedge_filter_ == &this_sensor) { |
| 394 | negedge_filter_ = nullptr; |
| 395 | } |
| 396 | return false; |
| 397 | } |
| 398 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 399 | bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge( |
| 400 | const constants::Values::Claws::AnglePair &angles, double *edge_encoder, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 401 | double *edge_angle, const HallEffectTracker &this_sensor, |
| 402 | const HallEffectTracker &sensorA, const HallEffectTracker &sensorB, |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 403 | const char *hall_effect_name) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 404 | bool found_edge = false; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 405 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 406 | if (SawFilteredPosedge(this_sensor, sensorA, sensorB)) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 407 | if (min_hall_effect_off_angle_ == max_hall_effect_off_angle_) { |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 408 | LOG(WARNING, "%s: Uncertain which side, rejecting posedge\n", name_); |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 409 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 410 | const double average_last_encoder = |
| 411 | (min_hall_effect_off_angle_ + max_hall_effect_off_angle_) / 2.0; |
| 412 | if (posedge_value_ < average_last_encoder) { |
| 413 | *edge_angle = angles.upper_decreasing_angle; |
| 414 | LOG(INFO, "%s Posedge upper of %s -> %f posedge: %f avg_encoder: %f\n", |
| 415 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 416 | average_last_encoder); |
| 417 | } else { |
| 418 | *edge_angle = angles.lower_angle; |
| 419 | LOG(INFO, "%s Posedge lower of %s -> %f posedge: %f avg_encoder: %f\n", |
| 420 | name_, hall_effect_name, *edge_angle, posedge_value_, |
| 421 | average_last_encoder); |
| 422 | } |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 423 | *edge_encoder = posedge_value_; |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 424 | found_edge = true; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | if (SawFilteredNegedge(this_sensor, sensorA, sensorB)) { |
| 429 | if (min_hall_effect_on_angle_ == max_hall_effect_on_angle_) { |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 430 | LOG(WARNING, "%s: Uncertain which side, rejecting negedge\n", name_); |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 431 | } else { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 432 | const double average_last_encoder = |
| 433 | (min_hall_effect_on_angle_ + max_hall_effect_on_angle_) / 2.0; |
| 434 | if (negedge_value_ > average_last_encoder) { |
| 435 | *edge_angle = angles.upper_angle; |
| 436 | LOG(INFO, "%s Negedge upper of %s -> %f negedge: %f avg_encoder: %f\n", |
| 437 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 438 | average_last_encoder); |
| 439 | } else { |
| 440 | *edge_angle = angles.lower_decreasing_angle; |
| 441 | LOG(INFO, "%s Negedge lower of %s -> %f negedge: %f avg_encoder: %f\n", |
| 442 | name_, hall_effect_name, *edge_angle, negedge_value_, |
| 443 | average_last_encoder); |
| 444 | } |
| 445 | *edge_encoder = negedge_value_; |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 446 | found_edge = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 447 | } |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 450 | return found_edge; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 453 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 454 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 455 | double *edge_angle) { |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 456 | if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, front_, |
| 457 | calibration_, back_, "front")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 458 | return true; |
| 459 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 460 | if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle, |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 461 | calibration_, front_, back_, "calibration")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 462 | return true; |
| 463 | } |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 464 | if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, back_, |
| 465 | calibration_, front_, "back")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 466 | return true; |
| 467 | } |
| 468 | return false; |
| 469 | } |
| 470 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 471 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 472 | double edge_angle) { |
| 473 | double old_offset = offset_; |
| 474 | offset_ = edge_angle - edge_encoder; |
| 475 | const double doffset = offset_ - old_offset; |
| 476 | motor_->ChangeTopOffset(doffset); |
| 477 | } |
| 478 | |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 479 | double TopZeroedStateFeedbackLoop::ComputeCalibrationChange(double edge_encoder, |
| 480 | double edge_angle) { |
| 481 | const double offset = edge_angle - edge_encoder; |
| 482 | const double doffset = offset - offset_; |
| 483 | return doffset; |
| 484 | } |
| 485 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 486 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 487 | double edge_angle) { |
| 488 | double old_offset = offset_; |
| 489 | offset_ = edge_angle - edge_encoder; |
| 490 | const double doffset = offset_ - old_offset; |
| 491 | motor_->ChangeBottomOffset(doffset); |
| 492 | } |
| 493 | |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 494 | double BottomZeroedStateFeedbackLoop::ComputeCalibrationChange( |
| 495 | double edge_encoder, double edge_angle) { |
| 496 | const double offset = edge_angle - edge_encoder; |
| 497 | const double doffset = offset - offset_; |
| 498 | return doffset; |
| 499 | } |
| 500 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 501 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 502 | claw_.ChangeTopOffset(doffset); |
| 503 | if (has_top_claw_goal_) { |
| 504 | top_claw_goal_ += doffset; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 509 | claw_.ChangeBottomOffset(doffset); |
| 510 | if (has_bottom_claw_goal_) { |
| 511 | bottom_claw_goal_ += doffset; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 516 | Y_(1, 0) += doffset; |
| 517 | X_hat(1, 0) += doffset; |
| 518 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 519 | } |
| 520 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 521 | Y_(0, 0) += doffset; |
| 522 | X_hat(0, 0) += doffset; |
| 523 | X_hat(1, 0) -= doffset; |
| 524 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 525 | } |
joe | 7376ff5 | 2014-02-16 18:28:42 -0800 | [diff] [blame] | 526 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 527 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 528 | const frc971::constants::Values &values) { |
| 529 | // first update position based on angle limit |
| 530 | |
| 531 | const double separation = *top_goal - *bottom_goal; |
| 532 | if (separation > values.claw.claw_max_separation) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 533 | const double dsep = (separation - values.claw.claw_max_separation) / 2.0; |
| 534 | *bottom_goal += dsep; |
| 535 | *top_goal -= dsep; |
| 536 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 537 | } |
| 538 | if (separation < values.claw.claw_min_separation) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 539 | const double dsep = (separation - values.claw.claw_min_separation) / 2.0; |
| 540 | *bottom_goal += dsep; |
| 541 | *top_goal -= dsep; |
| 542 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 543 | } |
| 544 | |
| 545 | // now move both goals in unison |
| 546 | if (*bottom_goal < values.claw.lower_claw.lower_limit) { |
| 547 | *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal; |
| 548 | *bottom_goal = values.claw.lower_claw.lower_limit; |
| 549 | } |
| 550 | if (*bottom_goal > values.claw.lower_claw.upper_limit) { |
| 551 | *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit; |
| 552 | *bottom_goal = values.claw.lower_claw.upper_limit; |
| 553 | } |
| 554 | |
| 555 | if (*top_goal < values.claw.upper_claw.lower_limit) { |
| 556 | *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal; |
| 557 | *top_goal = values.claw.upper_claw.lower_limit; |
| 558 | } |
| 559 | if (*top_goal > values.claw.upper_claw.upper_limit) { |
| 560 | *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit; |
| 561 | *top_goal = values.claw.upper_claw.upper_limit; |
| 562 | } |
| 563 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 564 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 565 | bool ClawMotor::is_ready() const { |
| 566 | return ( |
| 567 | (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 568 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 569 | (((::aos::robot_state.get() == NULL) ? true |
| 570 | : ::aos::robot_state->autonomous) && |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 571 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 572 | top_claw_.zeroing_state() == |
| 573 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 574 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 575 | bottom_claw_.zeroing_state() == |
| 576 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))); |
| 577 | } |
| 578 | |
| 579 | bool ClawMotor::is_zeroing() const { return !is_ready(); } |
| 580 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 581 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 582 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 583 | const control_loops::ClawGroup::Position *position, |
| 584 | control_loops::ClawGroup::Output *output, |
James Kuszmaul | 9ead1de | 2014-02-28 21:24:39 -0800 | [diff] [blame] | 585 | control_loops::ClawGroup::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 586 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 587 | |
| 588 | // Disable the motors now so that all early returns will return with the |
| 589 | // motors disabled. |
| 590 | if (output) { |
| 591 | output->top_claw_voltage = 0; |
| 592 | output->bottom_claw_voltage = 0; |
| 593 | output->intake_voltage = 0; |
Ben Fredrickson | 61893d5 | 2014-03-02 09:43:23 +0000 | [diff] [blame] | 594 | output->tusk_voltage = 0; |
| 595 | } |
| 596 | |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 597 | if (goal) { |
| 598 | if (::std::isnan(goal->bottom_angle) || |
| 599 | ::std::isnan(goal->separation_angle) || ::std::isnan(goal->intake) || |
| 600 | ::std::isnan(goal->centering)) { |
| 601 | return; |
| 602 | } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 605 | if (reset()) { |
Austin Schuh | 27b8fb1 | 2014-02-22 15:10:05 -0800 | [diff] [blame] | 606 | top_claw_.Reset(position->top); |
| 607 | bottom_claw_.Reset(position->bottom); |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 610 | const frc971::constants::Values &values = constants::GetValues(); |
| 611 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 612 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 613 | Eigen::Matrix<double, 2, 1> Y; |
| 614 | Y << position->bottom.position + bottom_claw_.offset(), |
| 615 | position->top.position + top_claw_.offset(); |
| 616 | claw_.Correct(Y); |
| 617 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 618 | top_claw_.SetPositionValues(position->top); |
| 619 | bottom_claw_.SetPositionValues(position->bottom); |
| 620 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 621 | if (!has_top_claw_goal_) { |
| 622 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 623 | top_claw_goal_ = top_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 624 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 625 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 626 | } |
| 627 | if (!has_bottom_claw_goal_) { |
| 628 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 629 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 630 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 631 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 632 | } |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 633 | LOG_STRUCT(DEBUG, "absolute position", |
| 634 | ClawPositionToLog(top_claw_.absolute_position(), |
| 635 | bottom_claw_.absolute_position())); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 638 | bool autonomous, enabled; |
| 639 | if (::aos::robot_state.get() == nullptr) { |
| 640 | autonomous = true; |
| 641 | enabled = false; |
| 642 | } else { |
| 643 | autonomous = ::aos::robot_state->autonomous; |
| 644 | enabled = ::aos::robot_state->enabled; |
| 645 | } |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 646 | |
| 647 | double bottom_claw_velocity_ = 0.0; |
| 648 | double top_claw_velocity_ = 0.0; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 649 | |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 650 | if (goal != NULL && |
| 651 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 652 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 653 | (autonomous && |
| 654 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 655 | top_claw_.zeroing_state() == |
| 656 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 657 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 658 | bottom_claw_.zeroing_state() == |
| 659 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION))))) { |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 660 | // Ready to use the claw. |
| 661 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 662 | bottom_claw_goal_ = goal->bottom_angle; |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 663 | top_claw_goal_ = goal->bottom_angle + goal->separation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 664 | has_bottom_claw_goal_ = true; |
| 665 | has_top_claw_goal_ = true; |
| 666 | doing_calibration_fine_tune_ = false; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 667 | mode_ = READY; |
Brian Silverman | 084372e | 2014-04-10 10:55:53 -0700 | [diff] [blame] | 668 | |
| 669 | bottom_claw_.HandleCalibrationError(values.claw.lower_claw); |
| 670 | top_claw_.HandleCalibrationError(values.claw.upper_claw); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 671 | } else if (top_claw_.zeroing_state() != |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 672 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 673 | bottom_claw_.zeroing_state() != |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 674 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 675 | // Time to fine tune the zero. |
| 676 | // Limit the goals here. |
Austin Schuh | 0c73342 | 2014-02-17 01:17:12 -0800 | [diff] [blame] | 677 | if (!enabled) { |
| 678 | // If we are disabled, start the fine tune process over again. |
| 679 | doing_calibration_fine_tune_ = false; |
| 680 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 681 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 682 | // always get the bottom claw to calibrated first |
| 683 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 684 | if (!doing_calibration_fine_tune_) { |
| 685 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 686 | values.claw.start_fine_tune_pos) < |
| 687 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 688 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 689 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 690 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 691 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 692 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 693 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 694 | } else { |
| 695 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 696 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 697 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 698 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 699 | } |
| 700 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 701 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 702 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 703 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 704 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 705 | if (top_claw_.front_or_back_triggered() || |
| 706 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 707 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 708 | // point and try again. |
| 709 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 710 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 711 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 712 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 713 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 714 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 715 | |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 716 | if (position && bottom_claw_.SawFilteredPosedge( |
| 717 | bottom_claw_.calibration(), bottom_claw_.front(), |
| 718 | bottom_claw_.back())) { |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 719 | // do calibration |
| 720 | bottom_claw_.SetCalibration( |
| 721 | position->bottom.posedge_value, |
| 722 | values.claw.lower_claw.calibration.lower_angle); |
| 723 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 724 | // calibrated so we are done fine tuning bottom |
| 725 | doing_calibration_fine_tune_ = false; |
| 726 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 727 | } else if (bottom_claw_.calibration().last_value()) { |
| 728 | doing_calibration_fine_tune_ = false; |
| 729 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
| 730 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
| 731 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 732 | } else { |
| 733 | LOG(DEBUG, "Fine tuning\n"); |
| 734 | } |
| 735 | } |
| 736 | // now set the top claw to track |
| 737 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 738 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 739 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 740 | // bottom claw must be calibrated, start on the top |
| 741 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 742 | if (::std::abs(top_absolute_position() - |
| 743 | values.claw.start_fine_tune_pos) < |
| 744 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 745 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 746 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 747 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 748 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 749 | LOG(DEBUG, "Ready to fine tune the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 750 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 751 | } else { |
| 752 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 753 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 754 | LOG(DEBUG, "Going to the start position for the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 755 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 756 | } |
| 757 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 758 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 759 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 760 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 761 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 762 | if (top_claw_.front_or_back_triggered() || |
| 763 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 764 | // this should not happen, but now we know it won't |
| 765 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 766 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 767 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 768 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 769 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 770 | } |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 771 | |
| 772 | if (position && |
| 773 | top_claw_.SawFilteredPosedge(top_claw_.calibration(), |
| 774 | top_claw_.front(), top_claw_.back())) { |
| 775 | // do calibration |
| 776 | top_claw_.SetCalibration( |
| 777 | position->top.posedge_value, |
| 778 | values.claw.upper_claw.calibration.lower_angle); |
| 779 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 780 | // calibrated so we are done fine tuning top |
| 781 | doing_calibration_fine_tune_ = false; |
| 782 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
| 783 | } else if (top_claw_.calibration().last_value()) { |
| 784 | doing_calibration_fine_tune_ = false; |
| 785 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
| 786 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
| 787 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 791 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 792 | } |
| 793 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 794 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 795 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 796 | if (position) { |
Brian Silverman | 7203569 | 2014-03-14 10:18:27 -0700 | [diff] [blame] | 797 | top_claw_goal_ = position->top.position + top_claw_.offset(); |
| 798 | bottom_claw_goal_ = position->bottom.position + bottom_claw_.offset(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 799 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 800 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 801 | } else { |
| 802 | has_top_claw_goal_ = false; |
| 803 | has_bottom_claw_goal_ = false; |
| 804 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 805 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 806 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 807 | if ((bottom_claw_.zeroing_state() != |
| 808 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 809 | bottom_claw_.front().value() || top_claw_.front().value()) && |
| 810 | !top_claw_.back().value() && !bottom_claw_.back().value()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 811 | if (enabled) { |
| 812 | // Time to slowly move back up to find any position to narrow down the |
| 813 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 814 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 815 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 816 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 817 | values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 818 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 819 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 820 | } else { |
| 821 | // We don't know where either claw is. Slowly start moving down to find |
| 822 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 823 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 824 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 825 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 826 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 827 | -values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 828 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
Ben Fredrickson | eaecbbb | 2014-02-23 12:12:03 +0000 | [diff] [blame] | 832 | if (position) { |
| 833 | if (enabled) { |
| 834 | top_claw_.SetCalibrationOnEdge( |
| 835 | values.claw.upper_claw, |
| 836 | ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 837 | bottom_claw_.SetCalibrationOnEdge( |
| 838 | values.claw.lower_claw, |
| 839 | ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
| 840 | } else { |
| 841 | // TODO(austin): Only calibrate on the predetermined edge. |
| 842 | // We might be able to just ignore this since the backlash is soooo |
| 843 | // low. |
| 844 | // :) |
| 845 | top_claw_.SetCalibrationOnEdge( |
| 846 | values.claw.upper_claw, |
| 847 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
| 848 | bottom_claw_.SetCalibrationOnEdge( |
| 849 | values.claw.lower_claw, |
| 850 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
| 851 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 852 | } |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 853 | mode_ = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 856 | // Limit the goals if both claws have been (mostly) found. |
| 857 | if (mode_ != UNKNOWN_LOCATION) { |
| 858 | LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values); |
| 859 | } |
| 860 | |
Brian Silverman | ed9df2f | 2014-04-05 07:07:15 -0700 | [diff] [blame] | 861 | claw_.set_positions_known(top_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION, bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::UNKNOWN_POSITION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 862 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 863 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, |
| 864 | bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 865 | double separation = -971; |
| 866 | if (position != nullptr) { |
| 867 | separation = position->top.position - position->bottom.position; |
| 868 | } |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 869 | LOG_STRUCT(DEBUG, "actual goal", |
| 870 | ClawGoalToLog(claw_.R(0, 0), claw_.R(1, 0), separation)); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 871 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 872 | // Only cap power when one of the halves of the claw is moving slowly and |
| 873 | // could wind up. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 874 | claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP || |
| 875 | mode_ == FINE_TUNE_BOTTOM); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 876 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 877 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 878 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 879 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 880 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 881 | capped_goal_ = false; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 882 | switch (mode_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 883 | case READY: |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 884 | case PREP_FINE_TUNE_TOP: |
| 885 | case PREP_FINE_TUNE_BOTTOM: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 886 | break; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 887 | case FINE_TUNE_BOTTOM: |
| 888 | case FINE_TUNE_TOP: |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 889 | case UNKNOWN_LOCATION: { |
Brian Silverman | 7b7c907 | 2014-03-30 13:33:30 -0700 | [diff] [blame] | 890 | LOG_MATRIX(DEBUG, "U_uncapped", claw_.U_uncapped); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 891 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 892 | double dx_bot = (claw_.U_uncapped(0, 0) - |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 893 | values.claw.max_zeroing_voltage) / |
| 894 | claw_.K(0, 0); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 895 | double dx_top = (claw_.U_uncapped(1, 0) - |
James Kuszmaul | 0e86651 | 2014-02-21 13:12:52 -0800 | [diff] [blame] | 896 | values.claw.max_zeroing_voltage) / |
| 897 | claw_.K(0, 0); |
| 898 | double dx = ::std::max(dx_top, dx_bot); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 899 | bottom_claw_goal_ -= dx; |
| 900 | top_claw_goal_ -= dx; |
James Kuszmaul | 0e86651 | 2014-02-21 13:12:52 -0800 | [diff] [blame] | 901 | Eigen::Matrix<double, 4, 1> R; |
| 902 | R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 903 | claw_.U = claw_.K() * (R - claw_.X_hat); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 904 | capped_goal_ = true; |
Brian Silverman | f48fab3 | 2014-03-09 14:32:24 -0700 | [diff] [blame] | 905 | LOG(DEBUG, "Moving the goal by %f to prevent windup." |
| 906 | " Uncapped is %f, max is %f, difference is %f\n", |
| 907 | dx, |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 908 | claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage, |
| 909 | (claw_.uncapped_average_voltage() - |
| 910 | values.claw.max_zeroing_voltage)); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 911 | } else if (claw_.uncapped_average_voltage() < |
| 912 | -values.claw.max_zeroing_voltage) { |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 913 | double dx_bot = (claw_.U_uncapped(0, 0) + |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 914 | values.claw.max_zeroing_voltage) / |
| 915 | claw_.K(0, 0); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 916 | double dx_top = (claw_.U_uncapped(1, 0) + |
James Kuszmaul | 0e86651 | 2014-02-21 13:12:52 -0800 | [diff] [blame] | 917 | values.claw.max_zeroing_voltage) / |
| 918 | claw_.K(0, 0); |
| 919 | double dx = ::std::min(dx_top, dx_bot); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 920 | bottom_claw_goal_ -= dx; |
| 921 | top_claw_goal_ -= dx; |
James Kuszmaul | 0e86651 | 2014-02-21 13:12:52 -0800 | [diff] [blame] | 922 | Eigen::Matrix<double, 4, 1> R; |
| 923 | R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0); |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 924 | claw_.U = claw_.K() * (R - claw_.X_hat); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 925 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 926 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 927 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 928 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (output) { |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 932 | if (goal) { |
| 933 | //setup the intake |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 934 | output->intake_voltage = |
| 935 | (goal->intake > 12.0) ? 12 : (goal->intake < -12.0) ? -12.0 |
| 936 | : goal->intake; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 937 | output->tusk_voltage = goal->centering; |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 938 | output->tusk_voltage = |
| 939 | (goal->centering > 12.0) ? 12 : (goal->centering < -12.0) |
| 940 | ? -12.0 |
| 941 | : goal->centering; |
Ben Fredrickson | 2f76ddf | 2014-02-23 05:58:23 +0000 | [diff] [blame] | 942 | } |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 943 | output->top_claw_voltage = claw_.U(1, 0); |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 944 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 945 | |
| 946 | if (output->top_claw_voltage > kMaxVoltage) { |
| 947 | output->top_claw_voltage = kMaxVoltage; |
| 948 | } else if (output->top_claw_voltage < -kMaxVoltage) { |
| 949 | output->top_claw_voltage = -kMaxVoltage; |
| 950 | } |
| 951 | |
| 952 | if (output->bottom_claw_voltage > kMaxVoltage) { |
| 953 | output->bottom_claw_voltage = kMaxVoltage; |
| 954 | } else if (output->bottom_claw_voltage < -kMaxVoltage) { |
| 955 | output->bottom_claw_voltage = -kMaxVoltage; |
| 956 | } |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 957 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 958 | |
James Kuszmaul | 4abaf48 | 2014-02-26 21:16:35 -0800 | [diff] [blame] | 959 | status->bottom = bottom_absolute_position(); |
| 960 | status->separation = top_absolute_position() - bottom_absolute_position(); |
| 961 | status->bottom_velocity = claw_.X_hat(2, 0); |
| 962 | status->separation_velocity = claw_.X_hat(3, 0); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 963 | |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 964 | if (goal) { |
| 965 | bool bottom_done = |
| 966 | ::std::abs(bottom_absolute_position() - goal->bottom_angle) < 0.020; |
| 967 | bool bottom_velocity_done = ::std::abs(status->bottom_velocity) < 0.2; |
| 968 | bool separation_done = |
| 969 | ::std::abs((top_absolute_position() - bottom_absolute_position()) - |
| 970 | goal->separation_angle) < 0.020; |
| 971 | bool separation_done_with_ball = |
| 972 | ::std::abs((top_absolute_position() - bottom_absolute_position()) - |
| 973 | goal->separation_angle) < 0.06; |
| 974 | status->done = is_ready() && separation_done && bottom_done && bottom_velocity_done; |
| 975 | status->done_with_ball = |
| 976 | is_ready() && separation_done_with_ball && bottom_done && bottom_velocity_done; |
| 977 | } else { |
| 978 | status->done = status->done_with_ball = false; |
| 979 | } |
Austin Schuh | a556b01 | 2014-03-02 11:55:52 -0800 | [diff] [blame] | 980 | |
Austin Schuh | 4f8633f | 2014-03-02 13:59:46 -0800 | [diff] [blame] | 981 | status->zeroed = is_ready(); |
Brian Silverman | 80fc94c | 2014-03-09 16:56:01 -0700 | [diff] [blame] | 982 | status->zeroed_for_auto = |
| 983 | (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 984 | top_claw_.zeroing_state() == |
| 985 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 986 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 987 | bottom_claw_.zeroing_state() == |
| 988 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | 4f8633f | 2014-03-02 13:59:46 -0800 | [diff] [blame] | 989 | |
Brian Silverman | 71fbee0 | 2014-03-13 17:24:54 -0700 | [diff] [blame] | 990 | was_enabled_ = enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | } // namespace control_loops |
| 994 | } // namespace frc971 |
Ben Fredrickson | 81ba2d5 | 2014-03-02 08:21:46 +0000 | [diff] [blame] | 995 | |