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() { |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 49 | uncapped_average_voltage_ = (U(0, 0) + U(1, 0)) / 2.0; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 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; |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 56 | U(1, 0) -= difference; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 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; |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 61 | U(1, 0) += difference; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | double max_value = |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 66 | ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0))); |
| 67 | double scalar = 12.0 / max_value; |
| 68 | bool bottom_big = (::std::abs(U(0, 0)) > 12.0) && |
| 69 | (::std::abs(U(0, 0)) > ::std::abs(U(1, 0))); |
| 70 | bool top_big = (::std::abs(U(1, 0)) > 12.0) && (!bottom_big); |
| 71 | double separation_voltage = U(1, 0) - U(0, 0) / 3.0000; |
| 72 | double u_top = U(1, 0); |
| 73 | double u_bottom = U(0, 0); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 74 | |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 75 | if (bottom_big) { |
| 76 | LOG(DEBUG, "Capping U because bottom is %f\n", max_value); |
| 77 | u_bottom *= scalar; |
| 78 | u_top = separation_voltage + u_bottom / 3.0000; |
| 79 | // If we can't maintain the separation, just clip it. |
| 80 | if (u_top > 12.0) u_top = 12.0; |
| 81 | else if (u_top < -12.0) u_top = -12.0; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 82 | } |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 83 | else if (top_big) { |
| 84 | LOG(DEBUG, "Capping U because top is %f\n", max_value); |
| 85 | u_top *= scalar; |
| 86 | u_bottom = (u_top - separation_voltage) * 3.0000; |
| 87 | if (u_bottom > 12.0) u_bottom = 12.0; |
| 88 | else if (u_bottom < -12.0) u_bottom = -12.0; |
| 89 | } |
| 90 | |
| 91 | U(0, 0) = u_bottom; |
| 92 | U(1, 0) = u_top; |
| 93 | |
| 94 | LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0)); |
| 95 | LOG(DEBUG, "Separation Voltage was %f, is now %f\n", separation_voltage, |
| 96 | U(1, 0) - U(0, 0) / 3.0000); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 99 | ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw) |
| 100 | : aos::control_loops::ControlLoop<control_loops::ClawGroup>(my_claw), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 101 | has_top_claw_goal_(false), |
| 102 | top_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 103 | top_claw_(this), |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 104 | has_bottom_claw_goal_(false), |
| 105 | bottom_claw_goal_(0.0), |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 106 | bottom_claw_(this), |
| 107 | claw_(MakeClawLoop()), |
Ben Fredrickson | 9b38842 | 2014-02-13 06:15:31 +0000 | [diff] [blame] | 108 | was_enabled_(false), |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 109 | doing_calibration_fine_tune_(false), |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 110 | capped_goal_(false), |
| 111 | mode_(UNKNOWN_LOCATION) {} |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 112 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 113 | const int ZeroedStateFeedbackLoop::kZeroingMaxVoltage; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 114 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 115 | bool ZeroedStateFeedbackLoop::DoGetPositionOfEdge( |
| 116 | const constants::Values::Claws::AnglePair &angles, double *edge_encoder, |
| 117 | double *edge_angle, const HallEffectTracker &sensor, |
| 118 | const char *hall_effect_name) { |
| 119 | if (sensor.posedge_count_changed()) { |
| 120 | if (posedge_value_ < last_encoder()) { |
| 121 | *edge_angle = angles.upper_angle; |
| 122 | LOG(INFO, "%s Posedge upper of %s -> %f\n", name_, |
| 123 | hall_effect_name, *edge_angle); |
| 124 | } else { |
| 125 | *edge_angle = angles.lower_angle; |
| 126 | LOG(INFO, "%s Posedge lower of %s -> %f\n", name_, |
| 127 | hall_effect_name, *edge_angle); |
| 128 | } |
| 129 | *edge_encoder = posedge_value_; |
| 130 | return true; |
| 131 | } |
| 132 | if (sensor.negedge_count_changed()) { |
| 133 | if (negedge_value_ > last_encoder()) { |
| 134 | *edge_angle = angles.upper_angle; |
| 135 | LOG(INFO, "%s Negedge upper of %s -> %f\n", name_, |
| 136 | hall_effect_name, *edge_angle); |
| 137 | } else { |
| 138 | *edge_angle = angles.lower_angle; |
| 139 | LOG(INFO, "%s Negedge lower of %s -> %f\n", name_, |
| 140 | hall_effect_name, *edge_angle); |
| 141 | } |
| 142 | *edge_encoder = negedge_value_; |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 149 | bool ZeroedStateFeedbackLoop::GetPositionOfEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 150 | const constants::Values::Claws::Claw &claw_values, double *edge_encoder, |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 151 | double *edge_angle) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 152 | // TODO(austin): Validate that the hall effect edge makes sense. |
| 153 | // We must now be on the side of the edge that we expect to be, and the |
| 154 | // encoder must have been on either side of the edge before and after. |
| 155 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 156 | // TODO(austin): Compute the last off range min and max and compare the edge |
| 157 | // value to the middle of the range. This will be quite a bit more reliable. |
| 158 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 159 | if (DoGetPositionOfEdge(claw_values.front, edge_encoder, edge_angle, |
| 160 | front_, "front")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 163 | if (DoGetPositionOfEdge(claw_values.calibration, edge_encoder, edge_angle, |
| 164 | calibration_, "calibration")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 167 | if (DoGetPositionOfEdge(claw_values.back, edge_encoder, edge_angle, |
| 168 | back_, "back")) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 174 | void TopZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 175 | double edge_angle) { |
| 176 | double old_offset = offset_; |
| 177 | offset_ = edge_angle - edge_encoder; |
| 178 | const double doffset = offset_ - old_offset; |
| 179 | motor_->ChangeTopOffset(doffset); |
| 180 | } |
| 181 | |
| 182 | void BottomZeroedStateFeedbackLoop::SetCalibration(double edge_encoder, |
| 183 | double edge_angle) { |
| 184 | double old_offset = offset_; |
| 185 | offset_ = edge_angle - edge_encoder; |
| 186 | const double doffset = offset_ - old_offset; |
| 187 | motor_->ChangeBottomOffset(doffset); |
| 188 | } |
| 189 | |
| 190 | void ClawMotor::ChangeTopOffset(double doffset) { |
| 191 | claw_.ChangeTopOffset(doffset); |
| 192 | if (has_top_claw_goal_) { |
| 193 | top_claw_goal_ += doffset; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void ClawMotor::ChangeBottomOffset(double doffset) { |
| 198 | claw_.ChangeBottomOffset(doffset); |
| 199 | if (has_bottom_claw_goal_) { |
| 200 | bottom_claw_goal_ += doffset; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void ClawLimitedLoop::ChangeTopOffset(double doffset) { |
| 205 | Y_(1, 0) += doffset; |
| 206 | X_hat(1, 0) += doffset; |
| 207 | LOG(INFO, "Changing top offset by %f\n", doffset); |
| 208 | } |
| 209 | void ClawLimitedLoop::ChangeBottomOffset(double doffset) { |
| 210 | Y_(0, 0) += doffset; |
| 211 | X_hat(0, 0) += doffset; |
| 212 | X_hat(1, 0) -= doffset; |
| 213 | LOG(INFO, "Changing bottom offset by %f\n", doffset); |
| 214 | } |
joe | 7376ff5 | 2014-02-16 18:28:42 -0800 | [diff] [blame] | 215 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 216 | void LimitClawGoal(double *bottom_goal, double *top_goal, |
| 217 | const frc971::constants::Values &values) { |
| 218 | // first update position based on angle limit |
| 219 | |
| 220 | const double separation = *top_goal - *bottom_goal; |
| 221 | if (separation > values.claw.claw_max_separation) { |
| 222 | LOG(DEBUG, "Greater than\n"); |
| 223 | const double dsep = (separation - values.claw.claw_max_separation) / 2.0; |
| 224 | *bottom_goal += dsep; |
| 225 | *top_goal -= dsep; |
| 226 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 227 | } |
| 228 | if (separation < values.claw.claw_min_separation) { |
| 229 | LOG(DEBUG, "Less than\n"); |
| 230 | const double dsep = (separation - values.claw.claw_min_separation) / 2.0; |
| 231 | *bottom_goal += dsep; |
| 232 | *top_goal -= dsep; |
| 233 | LOG(DEBUG, "Goals now bottom: %f, top: %f\n", *bottom_goal, *top_goal); |
| 234 | } |
| 235 | |
| 236 | // now move both goals in unison |
| 237 | if (*bottom_goal < values.claw.lower_claw.lower_limit) { |
| 238 | *top_goal += values.claw.lower_claw.lower_limit - *bottom_goal; |
| 239 | *bottom_goal = values.claw.lower_claw.lower_limit; |
| 240 | } |
| 241 | if (*bottom_goal > values.claw.lower_claw.upper_limit) { |
| 242 | *top_goal -= *bottom_goal - values.claw.lower_claw.upper_limit; |
| 243 | *bottom_goal = values.claw.lower_claw.upper_limit; |
| 244 | } |
| 245 | |
| 246 | if (*top_goal < values.claw.upper_claw.lower_limit) { |
| 247 | *bottom_goal += values.claw.upper_claw.lower_limit - *top_goal; |
| 248 | *top_goal = values.claw.upper_claw.lower_limit; |
| 249 | } |
| 250 | if (*top_goal > values.claw.upper_claw.upper_limit) { |
| 251 | *bottom_goal -= *top_goal - values.claw.upper_claw.upper_limit; |
| 252 | *top_goal = values.claw.upper_claw.upper_limit; |
| 253 | } |
| 254 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 255 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 256 | bool ClawMotor::is_ready() const { |
| 257 | return ( |
| 258 | (top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 259 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 260 | (::aos::robot_state->autonomous && |
| 261 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 262 | top_claw_.zeroing_state() == |
| 263 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 264 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 265 | bottom_claw_.zeroing_state() == |
| 266 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))); |
| 267 | } |
| 268 | |
| 269 | bool ClawMotor::is_zeroing() const { return !is_ready(); } |
| 270 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 271 | // Positive angle is up, and positive power is up. |
Austin Schuh | cc0bf31 | 2014-02-09 00:39:29 -0800 | [diff] [blame] | 272 | void ClawMotor::RunIteration(const control_loops::ClawGroup::Goal *goal, |
| 273 | const control_loops::ClawGroup::Position *position, |
| 274 | control_loops::ClawGroup::Output *output, |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 275 | ::aos::control_loops::Status *status) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 276 | constexpr double dt = 0.01; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 277 | |
| 278 | // Disable the motors now so that all early returns will return with the |
| 279 | // motors disabled. |
| 280 | if (output) { |
| 281 | output->top_claw_voltage = 0; |
| 282 | output->bottom_claw_voltage = 0; |
| 283 | output->intake_voltage = 0; |
| 284 | } |
| 285 | |
Austin Schuh | 1a49994 | 2014-02-17 01:51:58 -0800 | [diff] [blame] | 286 | if (reset()) { |
| 287 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION); |
| 288 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::UNKNOWN_POSITION); |
| 289 | } |
| 290 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 291 | if (::aos::robot_state.get() == nullptr) { |
| 292 | return; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 295 | const frc971::constants::Values &values = constants::GetValues(); |
| 296 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 297 | if (position) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 298 | Eigen::Matrix<double, 2, 1> Y; |
| 299 | Y << position->bottom.position + bottom_claw_.offset(), |
| 300 | position->top.position + top_claw_.offset(); |
| 301 | claw_.Correct(Y); |
| 302 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 303 | top_claw_.SetPositionValues(position->top); |
| 304 | bottom_claw_.SetPositionValues(position->bottom); |
| 305 | |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 306 | if (!has_top_claw_goal_) { |
| 307 | has_top_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 308 | top_claw_goal_ = top_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 309 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 310 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 311 | } |
| 312 | if (!has_bottom_claw_goal_) { |
| 313 | has_bottom_claw_goal_ = true; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 314 | bottom_claw_goal_ = bottom_claw_.absolute_position(); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 315 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 316 | top_claw_.absolute_position() - bottom_claw_.absolute_position(); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 317 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 318 | LOG(DEBUG, "Claw position is (top: %f bottom: %f\n", |
| 319 | top_claw_.absolute_position(), bottom_claw_.absolute_position()); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 322 | const bool autonomous = ::aos::robot_state->autonomous; |
| 323 | const bool enabled = ::aos::robot_state->enabled; |
| 324 | |
| 325 | double bottom_claw_velocity_ = 0.0; |
| 326 | double top_claw_velocity_ = 0.0; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 327 | |
| 328 | if ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED && |
| 329 | bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED) || |
| 330 | (autonomous && |
| 331 | ((top_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 332 | top_claw_.zeroing_state() == |
| 333 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION) && |
| 334 | (bottom_claw_.zeroing_state() == ZeroedStateFeedbackLoop::CALIBRATED || |
| 335 | bottom_claw_.zeroing_state() == |
| 336 | ZeroedStateFeedbackLoop::DISABLED_CALIBRATION)))) { |
| 337 | // Ready to use the claw. |
| 338 | // Limit the goals here. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 339 | bottom_claw_goal_ = goal->bottom_angle; |
Brian Silverman | 7c021c4 | 2014-02-17 15:15:56 -0800 | [diff] [blame] | 340 | top_claw_goal_ = goal->bottom_angle + goal->separation_angle; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 341 | has_bottom_claw_goal_ = true; |
| 342 | has_top_claw_goal_ = true; |
| 343 | doing_calibration_fine_tune_ = false; |
| 344 | |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 345 | mode_ = READY; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 346 | } else if (top_claw_.zeroing_state() != |
| 347 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION && |
| 348 | bottom_claw_.zeroing_state() != |
| 349 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION) { |
| 350 | // Time to fine tune the zero. |
| 351 | // Limit the goals here. |
Austin Schuh | 0c73342 | 2014-02-17 01:17:12 -0800 | [diff] [blame] | 352 | if (!enabled) { |
| 353 | // If we are disabled, start the fine tune process over again. |
| 354 | doing_calibration_fine_tune_ = false; |
| 355 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 356 | if (bottom_claw_.zeroing_state() != ZeroedStateFeedbackLoop::CALIBRATED) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 357 | // always get the bottom claw to calibrated first |
| 358 | LOG(DEBUG, "Calibrating the bottom of the claw\n"); |
| 359 | if (!doing_calibration_fine_tune_) { |
| 360 | if (::std::abs(bottom_absolute_position() - |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 361 | values.claw.start_fine_tune_pos) < |
| 362 | values.claw.claw_unimportant_epsilon) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 363 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 364 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 365 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 366 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 367 | LOG(DEBUG, "Ready to fine tune the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 368 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 369 | } else { |
| 370 | // send bottom to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 371 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 372 | LOG(DEBUG, "Going to the start position for the bottom\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 373 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 374 | } |
| 375 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 376 | mode_ = FINE_TUNE_BOTTOM; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 377 | bottom_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 378 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 379 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 380 | if (top_claw_.front_or_back_triggered() || |
| 381 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 382 | // We shouldn't hit a limit, but if we do, go back to the zeroing |
| 383 | // point and try again. |
| 384 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 385 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 386 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 387 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 388 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 389 | } |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 390 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 391 | if (bottom_claw_.calibration().value()) { |
| 392 | if (bottom_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 393 | position) { |
| 394 | // do calibration |
| 395 | bottom_claw_.SetCalibration( |
| 396 | position->bottom.posedge_value, |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 397 | values.claw.lower_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 398 | bottom_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
| 399 | // calibrated so we are done fine tuning bottom |
| 400 | doing_calibration_fine_tune_ = false; |
| 401 | LOG(DEBUG, "Calibrated the bottom correctly!\n"); |
| 402 | } else { |
| 403 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 404 | bottom_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 405 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 406 | mode_ = PREP_FINE_TUNE_BOTTOM; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 407 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 408 | } else { |
| 409 | LOG(DEBUG, "Fine tuning\n"); |
| 410 | } |
| 411 | } |
| 412 | // now set the top claw to track |
| 413 | |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 414 | top_claw_goal_ = bottom_claw_goal_ + values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 415 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 416 | // bottom claw must be calibrated, start on the top |
| 417 | if (!doing_calibration_fine_tune_) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 418 | if (::std::abs(top_absolute_position() - |
| 419 | values.claw.start_fine_tune_pos) < |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 420 | values.claw.claw_unimportant_epsilon) {//HERE |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 421 | doing_calibration_fine_tune_ = true; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 422 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 423 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 424 | values.claw.claw_zeroing_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 425 | LOG(DEBUG, "Ready to fine tune the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 426 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 427 | } else { |
| 428 | // send top to zeroing start |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 429 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 430 | LOG(DEBUG, "Going to the start position for the top\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 431 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 432 | } |
| 433 | } else { |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 434 | mode_ = FINE_TUNE_TOP; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 435 | top_claw_goal_ += values.claw.claw_zeroing_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 436 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 437 | values.claw.claw_zeroing_speed; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 438 | if (top_claw_.front_or_back_triggered() || |
| 439 | bottom_claw_.front_or_back_triggered()) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 440 | // this should not happen, but now we know it won't |
| 441 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 442 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 443 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 444 | LOG(DEBUG, "Found a limit, starting over.\n"); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 445 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 446 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 447 | if (top_claw_.calibration().value()) { |
| 448 | if (top_claw_.calibration().posedge_count_changed() && |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 449 | position) { |
| 450 | // do calibration |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 451 | top_claw_.SetCalibration( |
| 452 | position->top.posedge_value, |
| 453 | values.claw.upper_claw.calibration.lower_angle); |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 454 | top_claw_.set_zeroing_state(ZeroedStateFeedbackLoop::CALIBRATED); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 455 | // calibrated so we are done fine tuning top |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 456 | doing_calibration_fine_tune_ = false; |
| 457 | LOG(DEBUG, "Calibrated the top correctly!\n"); |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 458 | } else { //HERE |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 459 | doing_calibration_fine_tune_ = false; |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 460 | top_claw_goal_ = values.claw.start_fine_tune_pos; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 461 | top_claw_velocity_ = bottom_claw_velocity_ = 0.0; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 462 | mode_ = PREP_FINE_TUNE_TOP; |
Austin Schuh | 288c8c3 | 2014-02-16 17:20:17 -0800 | [diff] [blame] | 463 | } |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | // now set the bottom claw to track |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 467 | bottom_claw_goal_ = top_claw_goal_ - values.claw.claw_zeroing_separation; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 468 | } |
| 469 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 470 | doing_calibration_fine_tune_ = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 471 | if (!was_enabled_ && enabled) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 472 | if (position) { |
| 473 | top_claw_goal_ = position->top.position; |
| 474 | bottom_claw_goal_ = position->bottom.position; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 475 | initial_separation_ = |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 476 | position->top.position - position->bottom.position; |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 477 | } else { |
| 478 | has_top_claw_goal_ = false; |
| 479 | has_bottom_claw_goal_ = false; |
| 480 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 481 | } |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 482 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 483 | if ((bottom_claw_.zeroing_state() != |
| 484 | ZeroedStateFeedbackLoop::UNKNOWN_POSITION || |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 485 | bottom_claw_.front().value() || top_claw_.front().value()) && |
| 486 | !top_claw_.back().value() && !bottom_claw_.back().value()) { |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 487 | if (enabled) { |
| 488 | // Time to slowly move back up to find any position to narrow down the |
| 489 | // zero. |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 490 | top_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
| 491 | bottom_claw_goal_ += values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 492 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 493 | values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 494 | LOG(DEBUG, "Bottom is known.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 495 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 496 | } else { |
| 497 | // We don't know where either claw is. Slowly start moving down to find |
| 498 | // any hall effect. |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 499 | if (enabled) { |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 500 | top_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
| 501 | bottom_claw_goal_ -= values.claw.claw_zeroing_off_speed * dt; |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 502 | top_claw_velocity_ = bottom_claw_velocity_ = |
| 503 | -values.claw.claw_zeroing_off_speed; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 504 | LOG(DEBUG, "Both are unknown.\n"); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | if (enabled) { |
| 509 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 510 | values.claw.upper_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 511 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 512 | values.claw.lower_claw, ZeroedStateFeedbackLoop::APPROXIMATE_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 513 | } else { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 514 | // TODO(austin): Only calibrate on the predetermined edge. |
| 515 | // We might be able to just ignore this since the backlash is soooo low. :) |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 516 | top_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 517 | values.claw.upper_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 518 | bottom_claw_.SetCalibrationOnEdge( |
Austin Schuh | d27931c | 2014-02-16 19:18:20 -0800 | [diff] [blame] | 519 | values.claw.lower_claw, ZeroedStateFeedbackLoop::DISABLED_CALIBRATION); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 520 | } |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 521 | mode_ = UNKNOWN_LOCATION; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 524 | // Limit the goals if both claws have been (mostly) found. |
| 525 | if (mode_ != UNKNOWN_LOCATION) { |
| 526 | LimitClawGoal(&bottom_claw_goal_, &top_claw_goal_, values); |
| 527 | } |
| 528 | |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 529 | if (has_top_claw_goal_ && has_bottom_claw_goal_) { |
Austin Schuh | 069143b | 2014-02-17 02:46:26 -0800 | [diff] [blame] | 530 | claw_.R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, |
| 531 | bottom_claw_velocity_, top_claw_velocity_ - bottom_claw_velocity_; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 532 | double separation = -971; |
| 533 | if (position != nullptr) { |
| 534 | separation = position->top.position - position->bottom.position; |
| 535 | } |
| 536 | LOG(DEBUG, "Goal is %f (bottom) %f, separation is %f\n", claw_.R(0, 0), |
| 537 | claw_.R(1, 0), separation); |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 538 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 539 | // Only cap power when one of the halves of the claw is unknown. |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 540 | claw_.set_is_zeroing(mode_ == UNKNOWN_LOCATION || mode_ == FINE_TUNE_TOP || |
| 541 | mode_ == FINE_TUNE_BOTTOM); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 542 | claw_.Update(output == nullptr); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 543 | } else { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 544 | claw_.Update(true); |
Austin Schuh | f9286cd | 2014-02-11 00:51:09 -0800 | [diff] [blame] | 545 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 546 | |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 547 | capped_goal_ = false; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 548 | switch (mode_) { |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 549 | case READY: |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 550 | case PREP_FINE_TUNE_TOP: |
| 551 | case PREP_FINE_TUNE_BOTTOM: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 552 | break; |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 553 | case FINE_TUNE_BOTTOM: |
| 554 | case FINE_TUNE_TOP: |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 555 | case UNKNOWN_LOCATION: { |
| 556 | if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) { |
| 557 | double dx = (claw_.uncapped_average_voltage() - |
| 558 | values.claw.max_zeroing_voltage) / |
| 559 | claw_.K(0, 0); |
| 560 | bottom_claw_goal_ -= dx; |
| 561 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 562 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 563 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | e7f90d1 | 2014-02-17 00:48:25 -0800 | [diff] [blame] | 564 | LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n", |
| 565 | claw_.uncapped_average_voltage(), values.claw.max_zeroing_voltage, |
| 566 | (claw_.uncapped_average_voltage() - |
| 567 | values.claw.max_zeroing_voltage)); |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 568 | } else if (claw_.uncapped_average_voltage() < |
| 569 | -values.claw.max_zeroing_voltage) { |
| 570 | double dx = (claw_.uncapped_average_voltage() + |
| 571 | values.claw.max_zeroing_voltage) / |
| 572 | claw_.K(0, 0); |
| 573 | bottom_claw_goal_ -= dx; |
| 574 | top_claw_goal_ -= dx; |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 575 | capped_goal_ = true; |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 576 | LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 577 | } |
Austin Schuh | 4cb047f | 2014-02-16 21:10:19 -0800 | [diff] [blame] | 578 | } break; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | if (output) { |
James Kuszmaul | d536a40 | 2014-02-18 22:32:12 -0800 | [diff] [blame] | 582 | output->top_claw_voltage = claw_.U(1, 0); |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 583 | output->bottom_claw_voltage = claw_.U(0, 0); |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 584 | } |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 585 | status->done = false; |
Austin Schuh | 4b7b5d0 | 2014-02-10 21:20:34 -0800 | [diff] [blame] | 586 | |
| 587 | was_enabled_ = ::aos::robot_state->enabled; |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | } // namespace control_loops |
| 591 | } // namespace frc971 |