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