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