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