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