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 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 90 | bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge( |
| 91 | const constants::Values::Claws::AnglePair &angles, double *edge_encoder, |
| 92 | double *edge_angle, const HallEffectTracker &sensor, |
| 93 | const char *hall_effect_name) { |
| 94 | if (sensor.posedge_count_changed()) { |
| 95 | if (posedge_value_ < last_encoder()) { |
| 96 | *edge_angle = angles.upper_angle; |
| 97 | LOG(INFO, "%s Posedge upper of %s -> %f\n", name_, |
| 98 | hall_effect_name, *edge_angle); |
| 99 | } else { |
| 100 | *edge_angle = angles.lower_angle; |
| 101 | LOG(INFO, "%s Posedge lower of %s -> %f\n", name_, |
| 102 | hall_effect_name, *edge_angle); |
| 103 | } |
| 104 | *edge_encoder = posedge_value_; |
| 105 | return true; |
| 106 | } |
| 107 | if (sensor.negedge_count_changed()) { |
| 108 | if (negedge_value_ > last_encoder()) { |
| 109 | *edge_angle = angles.upper_angle; |
| 110 | LOG(INFO, "%s Negedge upper of %s -> %f\n", name_, |
| 111 | hall_effect_name, *edge_angle); |
| 112 | } else { |
| 113 | *edge_angle = angles.lower_angle; |
| 114 | LOG(INFO, "%s Negedge lower of %s -> %f\n", name_, |
| 115 | hall_effect_name, *edge_angle); |
| 116 | } |
| 117 | *edge_encoder = negedge_value_; |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 124 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 125 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 126 | double *edge_angle) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 127 | // TODO(austin): Validate that the hall effect edge makes sense. |
| 128 | // We must now be on the side of the edge that we expect to be, and the |
| 129 | // encoder must have been on either side of the edge before and after. |
| 130 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 131 | // TODO(austin): Compute the last off range min and max and compare the edge |
| 132 | // value to the middle of the range. This will be quite a bit more reliable. |
| 133 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 134 | if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, |
| 135 | front_, "front")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 136 | return true; |
| 137 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 138 | if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle, |
| 139 | calibration_, "calibration")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 140 | return true; |
| 141 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 142 | if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, |
| 143 | back_, "back")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 149 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 150 | double edge_angle) { |
| 151 | double old_offset = offset_; |
| 152 | offset_ = edge_angle - edge_encoder; |
| 153 | const double doffset = offset_ - old_offset; |
| 154 | motor_->ChangeTopOffset(doffset); |
| 155 | } |
| 156 | |
| 157 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 158 | double edge_angle) { |
| 159 | double old_offset = offset_; |
| 160 | offset_ = edge_angle - edge_encoder; |
| 161 | const double doffset = offset_ - old_offset; |
| 162 | motor_->ChangeBottomOffset(doffset); |
| 163 | } |
| 164 | |
| 165 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 166 | claw_.ChangeTopOffset(doffset); |
| 167 | if (has_top_claw_goal_) { |
| 168 | top_claw_goal_ += doffset; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 173 | claw_.ChangeBottomOffset(doffset); |
| 174 | if (has_bottom_claw_goal_) { |
| 175 | bottom_claw_goal_ += doffset; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 180 | Y_(1, 0) += doffset; |
| 181 | X_hat(1, 0) += doffset; |
| 182 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 183 | } |
| 184 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 185 | Y_(0, 0) += doffset; |
| 186 | X_hat(0, 0) += doffset; |
| 187 | X_hat(1, 0) -= doffset; |
| 188 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 189 | } |
| 190 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 191 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 192 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 193 | const control_loops::ClawGroup::Position *position, |
| 194 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 195 | ::aos::control_loops::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 196 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 197 | |
| 198 | // Disable the motors now so that all early returns will return with the |
| 199 | // motors disabled. |
| 200 | if (output) { |
| 201 | output->top_claw_voltage = 0; |
| 202 | output->bottom_claw_voltage = 0; |
| 203 | output->intake_voltage = 0; |
| 204 | } |
| 205 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 206 | // TODO(austin): Handle the disabled state and the disabled -> enabled |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 207 | // transition in all of these states. |
| 208 | // TODO(austin): Handle zeroing while disabled correctly (only use a single |
| 209 | // edge and direction when zeroing.) |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 210 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 211 | if (::aos::robot_state.get() == nullptr) { |
| 212 | return; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 215 | const frc971::constants::Values &values = constants::GetValues(); |
| 216 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 217 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 218 | Eigen::Matrix<double, 2, 1> Y; |
| 219 | Y << position->bottom.position + bottom_claw_.offset(), |
| 220 | position->top.position + top_claw_.offset(); |
| 221 | claw_.Correct(Y); |
| 222 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 223 | top_claw_.SetPositionValues(position->top); |
| 224 | bottom_claw_.SetPositionValues(position->bottom); |
| 225 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 226 | if (!has_top_claw_goal_) { |
| 227 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 228 | top_claw_goal_ = top_claw_.absolute_position(); |
| 229 | initial_seperation_ = |
| 230 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 231 | } |
| 232 | if (!has_bottom_claw_goal_) { |
| 233 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 234 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
| 235 | initial_seperation_ = |
| 236 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 237 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 238 | LOG(DEBUG, "Claw position is (top: %f bottom: %f\n", |
| 239 | top_claw_.absolute_position(), bottom_claw_.absolute_position()); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool autonomous = ::aos::robot_state->autonomous; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 243 | bool enabled = ::aos::robot_state->enabled; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 244 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 245 | enum CalibrationMode { |
| 246 | READY, |
| 247 | FINE_TUNE, |
| 248 | UNKNOWN_LOCATION |
| 249 | }; |
| 250 | |
| 251 | CalibrationMode mode; |
| 252 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 253 | if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 254 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 255 | (autonomous && |
| 256 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 257 | top_claw_.zeroing_state() == |
| 258 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 259 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 260 | bottom_claw_.zeroing_state() == |
| 261 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) { |
| 262 | // Ready to use the claw. |
| 263 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 264 | bottom_claw_goal_ = goal->bottom_angle; |
| 265 | top_claw_goal_ = goal->bottom_angle + goal->seperation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 266 | has_bottom_claw_goal_ = true; |
| 267 | has_top_claw_goal_ = true; |
| 268 | doing_calibration_fine_tune_ = false; |
| 269 | |
| 270 | mode = READY; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 271 | } else if (top_claw_.zeroing_state() != |
| 272 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
| 273 | bottom_claw_.zeroing_state() != |
| 274 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
| 275 | // Time to fine tune the zero. |
| 276 | // Limit the goals here. |
| 277 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 278 | // always get the bottom claw to calibrated first |
| 279 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 280 | if (!doing_calibration_fine_tune_) { |
| 281 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 282 | values.claw.start_fine_tune_pos) < |
| 283 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 284 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 285 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 286 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
| 287 | } else { |
| 288 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 289 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 290 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
| 291 | } |
| 292 | } else { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 293 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 294 | if (top_claw_.front_or_back_triggered() || |
| 295 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 296 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 297 | // point and try again. |
| 298 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 299 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 300 | LOG(DEBUG, "Found a limit, starting over.\n"); |
| 301 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 302 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 303 | if (bottom_claw_.calibration().value()) { |
| 304 | if (bottom_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 305 | position) { |
| 306 | // do calibration |
| 307 | bottom_claw_.SetCalibration( |
| 308 | position->bottom.posedge_value, |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 309 | values.claw.lower_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 310 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 311 | // calibrated so we are done fine tuning bottom |
| 312 | doing_calibration_fine_tune_ = false; |
| 313 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 314 | } else { |
| 315 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 316 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 317 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 318 | } else { |
| 319 | LOG(DEBUG, "Fine tuning\n"); |
| 320 | } |
| 321 | } |
| 322 | // now set the top claw to track |
| 323 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 324 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 325 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 326 | // bottom claw must be calibrated, start on the top |
| 327 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 328 | if (::std::abs(top_absolute_position() - |
| 329 | values.claw.start_fine_tune_pos) < |
| 330 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 331 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 332 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 333 | LOG(DEBUG, "Ready to fine tune the top\n"); |
| 334 | } else { |
| 335 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 336 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 337 | LOG(DEBUG, "Going to the start position for the top\n"); |
| 338 | } |
| 339 | } else { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 340 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 341 | if (top_claw_.front_or_back_triggered() || |
| 342 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 343 | // this should not happen, but now we know it won't |
| 344 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 345 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 346 | LOG(DEBUG, "Found a limit, starting over.\n"); |
| 347 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 348 | if (top_claw_.calibration().value()) { |
| 349 | if (top_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 350 | position) { |
| 351 | // do calibration |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 352 | top_claw_.SetCalibration( |
| 353 | position->top.posedge_value, |
| 354 | values.claw.upper_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 355 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 356 | // calinrated so we are done fine tuning top |
| 357 | doing_calibration_fine_tune_ = false; |
| 358 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
| 359 | } else { |
| 360 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 361 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 362 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 366 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 367 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 368 | mode = FINE_TUNE; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 369 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 370 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 371 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 372 | if (position) { |
| 373 | top_claw_goal_ = position->top.position; |
| 374 | bottom_claw_goal_ = position->bottom.position; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 375 | initial_seperation_ = |
| 376 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 377 | } else { |
| 378 | has_top_claw_goal_ = false; |
| 379 | has_bottom_claw_goal_ = false; |
| 380 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 381 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 382 | |
| 383 | // TODO(austin): Limit the goals here. |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 384 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 385 | if ((bottom_claw_.zeroing_state() != |
| 386 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame^] | 387 | bottom_claw_.front().value() || top_claw_.front().value()) && |
| 388 | !top_claw_.back().value() && !bottom_claw_.back().value()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 389 | if (enabled) { |
| 390 | // Time to slowly move back up to find any position to narrow down the |
| 391 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 392 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 393 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 394 | // TODO(austin): Goal velocity too! |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 395 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 396 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 397 | } else { |
| 398 | // We don't know where either claw is. Slowly start moving down to find |
| 399 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 400 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 401 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 402 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 403 | // TODO(austin): Goal velocity too! |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 404 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | if (enabled) { |
| 409 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 410 | values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 411 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 412 | values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 413 | } else { |
| 414 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 415 | values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 416 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 417 | values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 418 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 419 | mode = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 422 | // TODO(austin): Handle disabled properly everwhere... Restart and all that |
| 423 | // jazz. |
| 424 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 425 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 426 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, 0, 0; |
| 427 | double separation = -971; |
| 428 | if (position != nullptr) { |
| 429 | separation = position->top.position - position->bottom.position; |
| 430 | } |
| 431 | LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0), |
| 432 | claw_.R(1, 0), separation); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 433 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 434 | // Only cap power when one of the halves of the claw is unknown. |
| 435 | claw_.set_is_zeroing(mode == UNKNOWN_LOCATION); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 436 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 437 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 438 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 439 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 440 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 441 | capped_goal_ = false; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 442 | switch (mode) { |
| 443 | case READY: |
| 444 | break; |
| 445 | case FINE_TUNE: |
| 446 | break; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 447 | case UNKNOWN_LOCATION: { |
| 448 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
| 449 | double dx = (claw_.uncapped_average_voltage() - |
| 450 | values.claw.max_zeroing_voltage) / |
| 451 | claw_.K(0, 0); |
| 452 | bottom_claw_goal_ -= dx; |
| 453 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 454 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 455 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
| 456 | } else if (claw_.uncapped_average_voltage() < |
| 457 | -values.claw.max_zeroing_voltage) { |
| 458 | double dx = (claw_.uncapped_average_voltage() + |
| 459 | values.claw.max_zeroing_voltage) / |
| 460 | claw_.K(0, 0); |
| 461 | bottom_claw_goal_ -= dx; |
| 462 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 463 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 464 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 465 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 466 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (output) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 470 | output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0); |
| 471 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 472 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 473 | status->done = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 474 | |
| 475 | was_enabled_ = ::aos::robot_state->enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | } // namespace control_loops |
| 479 | } // namespace frc971 |