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