Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/claw/claw.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | |
| 7 | #include "aos/common/control_loop/control_loops.q.h" |
| 8 | #include "aos/common/logging/logging.h" |
| 9 | |
| 10 | #include "frc971/constants.h" |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/claw/claw_motor_plant.h" |
| 12 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 13 | // Zeroing plan. |
| 14 | // There are 2 types of zeros. Enabled and disabled ones. |
| 15 | // Disabled ones are only valid during auto mode, and can be used to speed up |
| 16 | // the enabled zero process. We need to re-zero during teleop in case the auto |
| 17 | // zero was poor and causes us to miss all our shots. |
| 18 | // |
| 19 | // We need to be able to zero manually while disabled by moving the joint over |
| 20 | // the zeros. |
| 21 | // Zero on the down edge when disabled (gravity in the direction of motion) |
| 22 | // |
| 23 | // When enabled, zero on the up edge (gravity opposing the direction of motion) |
| 24 | // The enabled sequence needs to work as follows. We can crash the claw if we |
| 25 | // bring them too close to each other or too far from each other. The only safe |
| 26 | // thing to do is to move them in unison. |
| 27 | // |
| 28 | // Start by moving them both towards the front of the bot to either find either |
| 29 | // the middle hall effect on either jaw, or the front hall effect on the bottom |
| 30 | // jaw. Any edge that isn't the desired edge will provide an approximate edge |
| 31 | // location that can be used for the fine tuning step. |
| 32 | // Once an edge is found on the front claw, move back the other way with both |
| 33 | // claws until an edge is found for the other claw. |
| 34 | // Now that we have an approximate zero, we can robustify the limits to keep |
| 35 | // both claws safe. Then, we can move both claws to a position that is the |
| 36 | // correct side of the zero and go zero. |
| 37 | |
| 38 | // Valid region plan. |
| 39 | // Difference between the arms has a range, and the values of each arm has a range. |
| 40 | // If a claw runs up against a static limit, don't let the goal change outside |
| 41 | // the limit. |
| 42 | // If a claw runs up against a movable limit, move both claws outwards to get |
| 43 | // out of the condition. |
| 44 | |
| 45 | namespace frc971 { |
| 46 | namespace control_loops { |
| 47 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 48 | void ClawLimitedLoop::CapU() { |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 49 | uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0; |
| 50 | if (is_zeroing_) { |
| 51 | const frc971::constants::Values &values = constants::GetValues(); |
| 52 | if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) { |
| 53 | const double difference = |
| 54 | uncapped_average_voltage_ - values.claw.max_zeroing_voltage; |
| 55 | U(0, 0) -= difference; |
| 56 | U(1, 0) -= difference; |
| 57 | } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) { |
| 58 | const double difference = |
| 59 | -uncapped_average_voltage_ - values.claw.max_zeroing_voltage; |
| 60 | U(0, 0) += difference; |
| 61 | U(1, 0) += difference; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | double max_value = |
| 66 | ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0))); |
| 67 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 68 | if (max_value > 12.0) { |
| 69 | LOG(DEBUG, "Capping U because max is %f\n", max_value); |
| 70 | U = U * 12.0 / max_value; |
| 71 | LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0)); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 72 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 75 | ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw) |
| 76 | : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 77 | has_top_claw_goal_(false), |
| 78 | top_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 79 | top_claw_(this), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 80 | has_bottom_claw_goal_(false), |
| 81 | bottom_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 82 | bottom_claw_(this), |
| 83 | claw_(MakeClawLoop()), |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 84 | was_enabled_(false), |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 85 | doing_calibration_fine_tune_(false), |
| 86 | capped_goal_(false) {} |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 88 | const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 89 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 90 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 91 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 92 | double *edge_angle) { |
| 93 | |
| 94 | // TODO(austin): Validate that the hall effect edge makes sense. |
| 95 | // We must now be on the side of the edge that we expect to be, and the |
| 96 | // encoder must have been on either side of the edge before and after. |
| 97 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 98 | // TODO(austin): Compute the last off range min and max and compare the edge |
| 99 | // value to the middle of the range. This will be quite a bit more reliable. |
| 100 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 101 | if (front_hall_effect_posedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 102 | if (posedge_value_ - last_encoder() < 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 103 | *edge_angle = claw_values.front.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 104 | LOG(INFO, "%s Posedge front upper edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 105 | } else { |
| 106 | *edge_angle = claw_values.front.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 107 | LOG(INFO, "%s Posedge front lower edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 108 | } |
| 109 | *edge_encoder = posedge_value_; |
| 110 | return true; |
| 111 | } |
| 112 | if (front_hall_effect_negedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 113 | if (negedge_value_ - last_encoder() > 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 114 | *edge_angle = claw_values.front.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 115 | LOG(INFO, "%s Negedge front upper edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 116 | } else { |
| 117 | *edge_angle = claw_values.front.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 118 | LOG(INFO, "%s Negedge front lower edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 119 | } |
| 120 | *edge_encoder = negedge_value_; |
| 121 | return true; |
| 122 | } |
| 123 | if (calibration_hall_effect_posedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 124 | if (posedge_value_ - last_encoder() < 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 125 | *edge_angle = claw_values.calibration.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 126 | LOG(INFO, "%s Posedge calibration upper edge -> %f\n", name_, |
| 127 | *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 128 | } else { |
| 129 | *edge_angle = claw_values.calibration.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 130 | LOG(INFO, "%s Posedge calibration lower edge -> %f\n", name_, |
| 131 | *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 132 | } |
| 133 | *edge_encoder = posedge_value_; |
| 134 | return true; |
| 135 | } |
| 136 | if (calibration_hall_effect_negedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 137 | if (negedge_value_ - last_encoder() > 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 138 | *edge_angle = claw_values.calibration.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 139 | LOG(INFO, "%s Negedge calibration upper edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 140 | } else { |
| 141 | *edge_angle = claw_values.calibration.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 142 | LOG(INFO, "%s Negedge calibration lower edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 143 | } |
| 144 | *edge_encoder = negedge_value_; |
| 145 | return true; |
| 146 | } |
| 147 | if (back_hall_effect_posedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 148 | if (posedge_value_ - last_encoder() < 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 149 | *edge_angle = claw_values.back.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 150 | LOG(INFO, "%s Posedge back upper edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 151 | } else { |
| 152 | *edge_angle = claw_values.back.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 153 | LOG(INFO, "%s Posedge back lower edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 154 | } |
| 155 | *edge_encoder = posedge_value_; |
| 156 | return true; |
| 157 | } |
| 158 | if (back_hall_effect_negedge_count_changed()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 159 | if (negedge_value_ - last_encoder() > 0) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 160 | *edge_angle = claw_values.back.upper_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 161 | LOG(INFO, "%s Negedge back upper edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 162 | } else { |
| 163 | *edge_angle = claw_values.back.lower_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 164 | LOG(INFO, "%s Negedge back lower edge -> %f\n", name_, *edge_angle); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 165 | } |
| 166 | *edge_encoder = negedge_value_; |
| 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 172 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 173 | double edge_angle) { |
| 174 | double old_offset = offset_; |
| 175 | offset_ = edge_angle - edge_encoder; |
| 176 | const double doffset = offset_ - old_offset; |
| 177 | motor_->ChangeTopOffset(doffset); |
| 178 | } |
| 179 | |
| 180 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 181 | double edge_angle) { |
| 182 | double old_offset = offset_; |
| 183 | offset_ = edge_angle - edge_encoder; |
| 184 | const double doffset = offset_ - old_offset; |
| 185 | motor_->ChangeBottomOffset(doffset); |
| 186 | } |
| 187 | |
| 188 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 189 | claw_.ChangeTopOffset(doffset); |
| 190 | if (has_top_claw_goal_) { |
| 191 | top_claw_goal_ += doffset; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 196 | claw_.ChangeBottomOffset(doffset); |
| 197 | if (has_bottom_claw_goal_) { |
| 198 | bottom_claw_goal_ += doffset; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 203 | Y_(1, 0) += doffset; |
| 204 | X_hat(1, 0) += doffset; |
| 205 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 206 | } |
| 207 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 208 | Y_(0, 0) += doffset; |
| 209 | X_hat(0, 0) += doffset; |
| 210 | X_hat(1, 0) -= doffset; |
| 211 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 212 | } |
| 213 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 214 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 215 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 216 | const control_loops::ClawGroup::Position *position, |
| 217 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 218 | ::aos::control_loops::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 219 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 220 | |
| 221 | // Disable the motors now so that all early returns will return with the |
| 222 | // motors disabled. |
| 223 | if (output) { |
| 224 | output->top_claw_voltage = 0; |
| 225 | output->bottom_claw_voltage = 0; |
| 226 | output->intake_voltage = 0; |
| 227 | } |
| 228 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 229 | // TODO(austin): Handle the disabled state and the disabled -> enabled |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 230 | // transition in all of these states. |
| 231 | // TODO(austin): Handle zeroing while disabled correctly (only use a single |
| 232 | // edge and direction when zeroing.) |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 233 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 234 | if (::aos::robot_state.get() == nullptr) { |
| 235 | return; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 238 | const frc971::constants::Values &values = constants::GetValues(); |
| 239 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 240 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 241 | Eigen::Matrix<double, 2, 1> Y; |
| 242 | Y << position->bottom.position + bottom_claw_.offset(), |
| 243 | position->top.position + top_claw_.offset(); |
| 244 | claw_.Correct(Y); |
| 245 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 246 | top_claw_.SetPositionValues(position->top); |
| 247 | bottom_claw_.SetPositionValues(position->bottom); |
| 248 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 249 | if (!has_top_claw_goal_) { |
| 250 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 251 | top_claw_goal_ = top_claw_.absolute_position(); |
| 252 | initial_seperation_ = |
| 253 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 254 | } |
| 255 | if (!has_bottom_claw_goal_) { |
| 256 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 257 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
| 258 | initial_seperation_ = |
| 259 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 260 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 261 | LOG(DEBUG, "Claw position is (top: %f bottom: %f\n", |
| 262 | top_claw_.absolute_position(), bottom_claw_.absolute_position()); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | bool autonomous = ::aos::robot_state->autonomous; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 266 | bool enabled = ::aos::robot_state->enabled; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 267 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 268 | enum CalibrationMode { |
| 269 | READY, |
| 270 | FINE_TUNE, |
| 271 | UNKNOWN_LOCATION |
| 272 | }; |
| 273 | |
| 274 | CalibrationMode mode; |
| 275 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 276 | if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 277 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 278 | (autonomous && |
| 279 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 280 | top_claw_.zeroing_state() == |
| 281 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 282 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 283 | bottom_claw_.zeroing_state() == |
| 284 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) { |
| 285 | // Ready to use the claw. |
| 286 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 287 | bottom_claw_goal_ = goal->bottom_angle; |
| 288 | top_claw_goal_ = goal->bottom_angle + goal->seperation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 289 | has_bottom_claw_goal_ = true; |
| 290 | has_top_claw_goal_ = true; |
| 291 | doing_calibration_fine_tune_ = false; |
| 292 | |
| 293 | mode = READY; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 294 | } else if (top_claw_.zeroing_state() != |
| 295 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
| 296 | bottom_claw_.zeroing_state() != |
| 297 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
| 298 | // Time to fine tune the zero. |
| 299 | // Limit the goals here. |
| 300 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 301 | // always get the bottom claw to calibrated first |
| 302 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 303 | if (!doing_calibration_fine_tune_) { |
| 304 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 305 | values.claw.start_fine_tune_pos) < |
| 306 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 307 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 308 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 309 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
| 310 | } else { |
| 311 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 312 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 313 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
| 314 | } |
| 315 | } else { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 316 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 317 | if (top_claw_.front_hall_effect() || top_claw_.back_hall_effect() || |
| 318 | bottom_claw_.front_hall_effect() || |
| 319 | bottom_claw_.back_hall_effect()) { |
| 320 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 321 | // point and try again. |
| 322 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 323 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 324 | LOG(DEBUG, "Found a limit, starting over.\n"); |
| 325 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 326 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 327 | if (bottom_claw_.calibration_hall_effect()) { |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 328 | if (bottom_claw_.calibration_hall_effect_posedge_count_changed() && |
| 329 | position) { |
| 330 | // do calibration |
| 331 | bottom_claw_.SetCalibration( |
| 332 | position->bottom.posedge_value, |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 333 | values.claw.lower_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 334 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 335 | // calibrated so we are done fine tuning bottom |
| 336 | doing_calibration_fine_tune_ = false; |
| 337 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 338 | } else { |
| 339 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 340 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 341 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 342 | } else { |
| 343 | LOG(DEBUG, "Fine tuning\n"); |
| 344 | } |
| 345 | } |
| 346 | // now set the top claw to track |
| 347 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 348 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 349 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 350 | // bottom claw must be calibrated, start on the top |
| 351 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 352 | if (::std::abs(top_absolute_position() - |
| 353 | values.claw.start_fine_tune_pos) < |
| 354 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 355 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 356 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 357 | LOG(DEBUG, "Ready to fine tune the top\n"); |
| 358 | } else { |
| 359 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 360 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 361 | LOG(DEBUG, "Going to the start position for the top\n"); |
| 362 | } |
| 363 | } else { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 364 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 365 | if (top_claw_.front_hall_effect() || top_claw_.back_hall_effect() || |
| 366 | bottom_claw_.front_hall_effect() || |
| 367 | bottom_claw_.back_hall_effect()) { |
| 368 | // this should not happen, but now we know it won't |
| 369 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 370 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 371 | LOG(DEBUG, "Found a limit, starting over.\n"); |
| 372 | } |
| 373 | if (top_claw_.calibration_hall_effect()) { |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 374 | if (top_claw_.calibration_hall_effect_posedge_count_changed() && |
| 375 | position) { |
| 376 | // do calibration |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 377 | top_claw_.SetCalibration( |
| 378 | position->top.posedge_value, |
| 379 | values.claw.upper_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 380 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 381 | // calinrated so we are done fine tuning top |
| 382 | doing_calibration_fine_tune_ = false; |
| 383 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
| 384 | } else { |
| 385 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 386 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 387 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 391 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 392 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 393 | mode = FINE_TUNE; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 394 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 395 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 396 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 397 | if (position) { |
| 398 | top_claw_goal_ = position->top.position; |
| 399 | bottom_claw_goal_ = position->bottom.position; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 400 | initial_seperation_ = |
| 401 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 402 | } else { |
| 403 | has_top_claw_goal_ = false; |
| 404 | has_bottom_claw_goal_ = false; |
| 405 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 406 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 407 | |
| 408 | // TODO(austin): Limit the goals here. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 409 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 410 | if ((bottom_claw_.zeroing_state() != |
| 411 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
| 412 | bottom_claw_.front_hall_effect() || top_claw_.front_hall_effect()) && |
| 413 | !top_claw_.back_hall_effect() && !bottom_claw_.back_hall_effect()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 414 | if (enabled) { |
| 415 | // Time to slowly move back up to find any position to narrow down the |
| 416 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 417 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 418 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 419 | // TODO(austin): Goal velocity too! |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 420 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 421 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 422 | } else { |
| 423 | // We don't know where either claw is. Slowly start moving down to find |
| 424 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 425 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 426 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 427 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 428 | // TODO(austin): Goal velocity too! |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 429 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | if (enabled) { |
| 434 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 435 | values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 436 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 437 | values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 438 | } else { |
| 439 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 440 | values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 441 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 442 | values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 443 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 444 | mode = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 447 | // TODO(austin): Handle disabled properly everwhere... Restart and all that |
| 448 | // jazz. |
| 449 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 450 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 451 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, 0, 0; |
| 452 | double separation = -971; |
| 453 | if (position != nullptr) { |
| 454 | separation = position->top.position - position->bottom.position; |
| 455 | } |
| 456 | LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0), |
| 457 | claw_.R(1, 0), separation); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 458 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 459 | // Only cap power when one of the halves of the claw is unknown. |
| 460 | claw_.set_is_zeroing(mode == UNKNOWN_LOCATION); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 461 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 462 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 463 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 464 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 465 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 466 | capped_goal_ = false; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 467 | switch (mode) { |
| 468 | case READY: |
| 469 | break; |
| 470 | case FINE_TUNE: |
| 471 | break; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 472 | case UNKNOWN_LOCATION: { |
| 473 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
| 474 | double dx = (claw_.uncapped_average_voltage() - |
| 475 | values.claw.max_zeroing_voltage) / |
| 476 | claw_.K(0, 0); |
| 477 | bottom_claw_goal_ -= dx; |
| 478 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 479 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 480 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
| 481 | } else if (claw_.uncapped_average_voltage() < |
| 482 | -values.claw.max_zeroing_voltage) { |
| 483 | double dx = (claw_.uncapped_average_voltage() + |
| 484 | values.claw.max_zeroing_voltage) / |
| 485 | claw_.K(0, 0); |
| 486 | bottom_claw_goal_ -= dx; |
| 487 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 488 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 489 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 490 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 491 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | if (output) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 495 | output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0); |
| 496 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 497 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 498 | status->done = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 499 | |
| 500 | was_enabled_ = ::aos::robot_state->enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | } // namespace control_loops |
| 504 | } // namespace frc971 |