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